Skip to content

Commit 1ab306d

Browse files
author
Juanjo Alvarez
committed
Add context cancellation handling to Blame
Signed-off-by: Juanjo Alvarez <[email protected]>
1 parent eb7ee9b commit 1ab306d

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

internal/function/blame.go

+16-3
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,34 @@ import (
1313
)
1414

1515
type BlameGenerator struct {
16+
ctx *sql.Context
1617
commit *object.Commit
1718
file string
1819
curLine int
1920
lines []*git.Line
2021
}
2122

22-
func NewBlameGenerator(c *object.Commit, f string) (*BlameGenerator, error) {
23+
func NewBlameGenerator(ctx *sql.Context, c *object.Commit, f string) (*BlameGenerator, error) {
2324
result, err := git.Blame(c, f)
2425
if err != nil {
2526
return nil, err
2627
}
27-
return &BlameGenerator{commit: c, file: f, curLine: 0, lines: result.Lines}, nil
28+
return &BlameGenerator{
29+
ctx: ctx,
30+
commit: c,
31+
file: f,
32+
curLine: 0,
33+
lines: result.Lines,
34+
}, nil
2835
}
2936

3037
func (g *BlameGenerator) Next() (interface{}, error) {
38+
select {
39+
case <-g.ctx.Done():
40+
return nil, io.EOF
41+
default:
42+
}
43+
3144
if len(g.lines) == 0 || g.curLine >= len(g.lines) {
3245
return nil, io.EOF
3346
}
@@ -124,7 +137,7 @@ func (b *Blame) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
124137
return nil, nil
125138
}
126139

127-
bg, err := NewBlameGenerator(commit, file)
140+
bg, err := NewBlameGenerator(ctx, commit, file)
128141
if err != nil {
129142
ctx.Warn(0, err.Error())
130143
return nil, nil

0 commit comments

Comments
 (0)