Skip to content

Commit

Permalink
Add context cancellation handling to Blame
Browse files Browse the repository at this point in the history
Signed-off-by: Juanjo Alvarez <[email protected]>
  • Loading branch information
Juanjo Alvarez committed Oct 29, 2019
1 parent eb7ee9b commit 1ab306d
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions internal/function/blame.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,34 @@ import (
)

type BlameGenerator struct {
ctx *sql.Context
commit *object.Commit
file string
curLine int
lines []*git.Line
}

func NewBlameGenerator(c *object.Commit, f string) (*BlameGenerator, error) {
func NewBlameGenerator(ctx *sql.Context, c *object.Commit, f string) (*BlameGenerator, error) {
result, err := git.Blame(c, f)
if err != nil {
return nil, err
}
return &BlameGenerator{commit: c, file: f, curLine: 0, lines: result.Lines}, nil
return &BlameGenerator{
ctx: ctx,
commit: c,
file: f,
curLine: 0,
lines: result.Lines,
}, nil
}

func (g *BlameGenerator) Next() (interface{}, error) {
select {
case <-g.ctx.Done():
return nil, io.EOF
default:
}

if len(g.lines) == 0 || g.curLine >= len(g.lines) {
return nil, io.EOF
}
Expand Down Expand Up @@ -124,7 +137,7 @@ func (b *Blame) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
return nil, nil
}

bg, err := NewBlameGenerator(commit, file)
bg, err := NewBlameGenerator(ctx, commit, file)
if err != nil {
ctx.Warn(0, err.Error())
return nil, nil
Expand Down

0 comments on commit 1ab306d

Please sign in to comment.