diff options
author | Marc Barussaud <marc.barussaud@orange.com> | 2018-06-26 15:23:19 +0200 |
---|---|---|
committer | Santiago M. Mola <santi@mola.io> | 2018-07-02 15:11:59 +0200 |
commit | 9251ea764df3de13518f974635e76315b2b89e3e (patch) | |
tree | 9db44a52be37dd58e21b97861a9b3e1683449b24 /utils/merkletrie/difftree.go | |
parent | 662e2c226e9b8352a90cd1951233fab30a4e5042 (diff) | |
download | go-git-9251ea764df3de13518f974635e76315b2b89e3e.tar.gz |
plumbing: add context to allow cancel on diff/patch computing
Signed-off-by: Marc Barussaud <marc.barussaud@orange.com>
Diffstat (limited to 'utils/merkletrie/difftree.go')
-rw-r--r-- | utils/merkletrie/difftree.go | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/utils/merkletrie/difftree.go b/utils/merkletrie/difftree.go index 2294096..d57ed13 100644 --- a/utils/merkletrie/difftree.go +++ b/utils/merkletrie/difftree.go @@ -248,15 +248,30 @@ package merkletrie // h: else of i import ( + "context" + "errors" "fmt" "gopkg.in/src-d/go-git.v4/utils/merkletrie/noder" ) +var ( + ErrCanceled = errors.New("operation canceled") +) + // DiffTree calculates the list of changes between two merkletries. It // uses the provided hashEqual callback to compare noders. func DiffTree(fromTree, toTree noder.Noder, hashEqual noder.Equal) (Changes, error) { + return DiffTreeContext(context.Background(), fromTree, toTree, hashEqual) +} + +// DiffTree calculates the list of changes between two merkletries. It +// uses the provided hashEqual callback to compare noders. +// Error will be returned if context expires +// Provided context must be non nil +func DiffTreeContext(ctx context.Context, fromTree, toTree noder.Noder, + hashEqual noder.Equal) (Changes, error) { ret := NewChanges() ii, err := newDoubleIter(fromTree, toTree, hashEqual) @@ -265,6 +280,12 @@ func DiffTree(fromTree, toTree noder.Noder, } for { + select { + case <-ctx.Done(): + return nil, ErrCanceled + default: + } + from := ii.from.current to := ii.to.current |