aboutsummaryrefslogtreecommitdiffstats
path: root/diff/diff.go
diff options
context:
space:
mode:
authorAlberto Cortés <alberto@sourced.tech>2015-11-16 03:20:01 +0100
committerAlberto Cortés <alberto@sourced.tech>2015-11-25 11:09:51 +0100
commitd643cea1e8a6d618b2eddfdbed086c7bdf208658 (patch)
treeb862c72ccb674910d24eac2ab424a7fb5ea3f0fb /diff/diff.go
parentcaab43e7f4ee10a15b2af826485b688473b34356 (diff)
downloadgo-git-d643cea1e8a6d618b2eddfdbed086c7bdf208658.tar.gz
Blame support for files
This also includes a diff package and revlist package (needed by blame) Some extra packfiles (<1MB) are also included, to be used as fixtures in the tests.
Diffstat (limited to 'diff/diff.go')
-rw-r--r--diff/diff.go45
1 files changed, 45 insertions, 0 deletions
diff --git a/diff/diff.go b/diff/diff.go
new file mode 100644
index 0000000..b840ad6
--- /dev/null
+++ b/diff/diff.go
@@ -0,0 +1,45 @@
+// Package diff implements line oriented diffs, similar to the ancient
+// Unix diff command.
+//
+// The current implementation is just a wrapper around Sergi's
+// go-diff/diffmatchpatch library, which is a go port of Neil
+// Fraser's google-diff-match-patch code
+package diff
+
+import (
+ "bytes"
+
+ "github.com/sergi/go-diff/diffmatchpatch"
+)
+
+// Do computes the (line oriented) modifications needed to turn the src
+// string into the dst string.
+func Do(src, dst string) (diffs []diffmatchpatch.Diff) {
+ dmp := diffmatchpatch.New()
+ wSrc, wDst, warray := dmp.DiffLinesToChars(src, dst)
+ diffs = dmp.DiffMain(wSrc, wDst, false)
+ diffs = dmp.DiffCharsToLines(diffs, warray)
+ return diffs
+}
+
+// Dst computes and returns the destination text.
+func Dst(diffs []diffmatchpatch.Diff) string {
+ var text bytes.Buffer
+ for _, d := range diffs {
+ if d.Type != diffmatchpatch.DiffDelete {
+ text.WriteString(d.Text)
+ }
+ }
+ return text.String()
+}
+
+// Src computes and returns the source text
+func Src(diffs []diffmatchpatch.Diff) string {
+ var text bytes.Buffer
+ for _, d := range diffs {
+ if d.Type != diffmatchpatch.DiffInsert {
+ text.WriteString(d.Text)
+ }
+ }
+ return text.String()
+}