aboutsummaryrefslogtreecommitdiffstats
path: root/plumbing/object/patch.go
diff options
context:
space:
mode:
authorMarc Barussaud <marc.barussaud@orange.com>2018-06-26 15:23:19 +0200
committerSantiago M. Mola <santi@mola.io>2018-07-02 15:11:59 +0200
commit9251ea764df3de13518f974635e76315b2b89e3e (patch)
tree9db44a52be37dd58e21b97861a9b3e1683449b24 /plumbing/object/patch.go
parent662e2c226e9b8352a90cd1951233fab30a4e5042 (diff)
downloadgo-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 'plumbing/object/patch.go')
-rw-r--r--plumbing/object/patch.go32
1 files changed, 30 insertions, 2 deletions
diff --git a/plumbing/object/patch.go b/plumbing/object/patch.go
index aa96a96..adeaccb 100644
--- a/plumbing/object/patch.go
+++ b/plumbing/object/patch.go
@@ -2,6 +2,8 @@ package object
import (
"bytes"
+ "context"
+ "errors"
"fmt"
"io"
"math"
@@ -15,10 +17,25 @@ import (
dmp "github.com/sergi/go-diff/diffmatchpatch"
)
+var (
+ ErrCanceled = errors.New("operation canceled")
+)
+
func getPatch(message string, changes ...*Change) (*Patch, error) {
+ ctx := context.Background()
+ return getPatchContext(ctx, message, changes...)
+}
+
+func getPatchContext(ctx context.Context, message string, changes ...*Change) (*Patch, error) {
var filePatches []fdiff.FilePatch
for _, c := range changes {
- fp, err := filePatch(c)
+ select {
+ case <-ctx.Done():
+ return nil, ErrCanceled
+ default:
+ }
+
+ fp, err := filePatchWithContext(ctx, c)
if err != nil {
return nil, err
}
@@ -29,7 +46,7 @@ func getPatch(message string, changes ...*Change) (*Patch, error) {
return &Patch{message, filePatches}, nil
}
-func filePatch(c *Change) (fdiff.FilePatch, error) {
+func filePatchWithContext(ctx context.Context, c *Change) (fdiff.FilePatch, error) {
from, to, err := c.Files()
if err != nil {
return nil, err
@@ -52,6 +69,12 @@ func filePatch(c *Change) (fdiff.FilePatch, error) {
var chunks []fdiff.Chunk
for _, d := range diffs {
+ select {
+ case <-ctx.Done():
+ return nil, ErrCanceled
+ default:
+ }
+
var op fdiff.Operation
switch d.Type {
case dmp.DiffEqual:
@@ -70,6 +93,11 @@ func filePatch(c *Change) (fdiff.FilePatch, error) {
from: c.From,
to: c.To,
}, nil
+
+}
+
+func filePatch(c *Change) (fdiff.FilePatch, error) {
+ return filePatchWithContext(context.Background(), c)
}
func fileContent(f *File) (content string, isBinary bool, err error) {