diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2019-03-20 09:36:36 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-03-20 09:36:36 +0100 |
commit | e704ed55d364c1e5ceefbf47c4280c1904d6b1ca (patch) | |
tree | 24ece27667f4c41af3df3bcc04e2b0d1d68cbd21 /utils | |
parent | 948b0c930bf1b173b172e5f28aa71da454c3a093 (diff) | |
parent | a80a9e54409de2b17e4ca2e5a04bf4f0ac4b6fed (diff) | |
download | go-git-e704ed55d364c1e5ceefbf47c4280c1904d6b1ca.tar.gz |
Merge pull request #1084 from vmarkovtsev/master
Increase diffmatchcpatch timeout
Diffstat (limited to 'utils')
-rw-r--r-- | utils/diff/diff.go | 18 |
1 files changed, 17 insertions, 1 deletions
diff --git a/utils/diff/diff.go b/utils/diff/diff.go index f49ae55..6142ed0 100644 --- a/utils/diff/diff.go +++ b/utils/diff/diff.go @@ -8,14 +8,30 @@ package diff import ( "bytes" + "time" "github.com/sergi/go-diff/diffmatchpatch" ) // Do computes the (line oriented) modifications needed to turn the src -// string into the dst string. +// string into the dst string. The underlying algorithm is Meyers, +// its complexity is O(N*d) where N is min(lines(src), lines(dst)) and d +// is the size of the diff. func Do(src, dst string) (diffs []diffmatchpatch.Diff) { + // the default timeout is time.Second which may be too small under heavy load + return DoWithTimeout(src, dst, time.Hour) +} + +// DoWithTimeout computes the (line oriented) modifications needed to turn the src +// string into the dst string. The `timeout` argument specifies the maximum +// amount of time it is allowed to spend in this function. If the timeout +// is exceeded, the parts of the strings which were not considered are turned into +// a bulk delete+insert and the half-baked suboptimal result is returned at once. +// The underlying algorithm is Meyers, its complexity is O(N*d) where N is +// min(lines(src), lines(dst)) and d is the size of the diff. +func DoWithTimeout (src, dst string, timeout time.Duration) (diffs []diffmatchpatch.Diff) { dmp := diffmatchpatch.New() + dmp.DiffTimeout = timeout wSrc, wDst, warray := dmp.DiffLinesToRunes(src, dst) diffs = dmp.DiffMainRunes(wSrc, wDst, false) diffs = dmp.DiffCharsToLines(diffs, warray) |