|
| 1 | +package function |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/src-d/gitbase" |
| 7 | + "github.com/src-d/go-mysql-server/sql" |
| 8 | + "gopkg.in/src-d/go-git.v4" |
| 9 | + |
| 10 | + "gopkg.in/src-d/go-git.v4/plumbing" |
| 11 | + "gopkg.in/src-d/go-git.v4/plumbing/object" |
| 12 | +) |
| 13 | + |
| 14 | +type BlameGenerator struct { |
| 15 | + commit *object.Commit |
| 16 | + fIter *object.FileIter |
| 17 | + curLine int |
| 18 | + curFile *object.File |
| 19 | + lines []*git.Line |
| 20 | +} |
| 21 | + |
| 22 | +func NewBlameGenerator(c *object.Commit, f *object.FileIter) (*BlameGenerator, error) { |
| 23 | + return &BlameGenerator{commit: c, fIter: f, curLine: -1}, nil |
| 24 | +} |
| 25 | + |
| 26 | +func (g *BlameGenerator) loadNewFile() error { |
| 27 | + var err error |
| 28 | + g.curFile, err = g.fIter.Next() |
| 29 | + if err != nil { |
| 30 | + return err |
| 31 | + } |
| 32 | + |
| 33 | + result, err := git.Blame(g.commit, g.curFile.Name) |
| 34 | + if err != nil { |
| 35 | + return err |
| 36 | + } |
| 37 | + |
| 38 | + if len(result.Lines) == 0 { |
| 39 | + return g.loadNewFile() |
| 40 | + } |
| 41 | + |
| 42 | + g.lines = result.Lines |
| 43 | + g.curLine = 0 |
| 44 | + return nil |
| 45 | +} |
| 46 | + |
| 47 | +func (g *BlameGenerator) Next() (interface{}, error) { |
| 48 | + if g.curLine == -1 || g.curLine >= len(g.lines) { |
| 49 | + err := g.loadNewFile() |
| 50 | + if err != nil { |
| 51 | + return nil, err |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + l := g.lines[g.curLine] |
| 56 | + b := BlameLine{ |
| 57 | + Commit: g.commit.Hash.String(), |
| 58 | + File: g.curFile.Name, |
| 59 | + LineNum: g.curLine, |
| 60 | + Author: l.Author, |
| 61 | + Text: l.Text, |
| 62 | + } |
| 63 | + g.curLine++ |
| 64 | + return b, nil |
| 65 | +} |
| 66 | + |
| 67 | +func (g *BlameGenerator) Close() error { |
| 68 | + g.fIter.Close() |
| 69 | + return nil |
| 70 | +} |
| 71 | + |
| 72 | +var _ sql.Generator = (*BlameGenerator)(nil) |
| 73 | + |
| 74 | +type ( |
| 75 | + // Blame implements git-blame function as UDF |
| 76 | + Blame struct { |
| 77 | + repo sql.Expression |
| 78 | + commit sql.Expression |
| 79 | + } |
| 80 | + |
| 81 | + // BlameLine represents each line of git blame's output |
| 82 | + BlameLine struct { |
| 83 | + Commit string `json:"commit"` |
| 84 | + File string `json:"file"` |
| 85 | + LineNum int `json:"linenum"` |
| 86 | + Author string `json:"author"` |
| 87 | + Text string `json:"text"` |
| 88 | + } |
| 89 | +) |
| 90 | + |
| 91 | +// NewBlame constructor |
| 92 | +func NewBlame(repo, commit sql.Expression) sql.Expression { |
| 93 | + return &Blame{repo, commit} |
| 94 | +} |
| 95 | + |
| 96 | +func (b *Blame) String() string { |
| 97 | + return fmt.Sprintf("blame(%s, %s)", b.repo, b.commit) |
| 98 | +} |
| 99 | + |
| 100 | +// Type implements the sql.Expression interface |
| 101 | +func (*Blame) Type() sql.Type { |
| 102 | + return sql.Array(sql.JSON) |
| 103 | +} |
| 104 | + |
| 105 | +func (b *Blame) WithChildren(children ...sql.Expression) (sql.Expression, error) { |
| 106 | + if len(children) != 2 { |
| 107 | + return nil, sql.ErrInvalidChildrenNumber.New(b, len(children), 2) |
| 108 | + } |
| 109 | + |
| 110 | + return NewBlame(children[0], children[1]), nil |
| 111 | +} |
| 112 | + |
| 113 | +// Children implements the Expression interface. |
| 114 | +func (b *Blame) Children() []sql.Expression { |
| 115 | + return []sql.Expression{b.repo, b.commit} |
| 116 | +} |
| 117 | + |
| 118 | +// IsNullable implements the Expression interface. |
| 119 | +func (b *Blame) IsNullable() bool { |
| 120 | + return b.repo.IsNullable() || (b.commit.IsNullable()) |
| 121 | +} |
| 122 | + |
| 123 | +// Resolved implements the Expression interface. |
| 124 | +func (b *Blame) Resolved() bool { |
| 125 | + return b.repo.Resolved() && b.commit.Resolved() |
| 126 | +} |
| 127 | + |
| 128 | +// Eval implements the sql.Expression interface. |
| 129 | +func (b *Blame) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) { |
| 130 | + span, ctx := ctx.Span("gitbase.Blame") |
| 131 | + defer span.Finish() |
| 132 | + |
| 133 | + repo, err := b.resolveRepo(ctx, row) |
| 134 | + if err != nil { |
| 135 | + ctx.Warn(0, err.Error()) |
| 136 | + return nil, nil |
| 137 | + } |
| 138 | + |
| 139 | + commit, err := b.resolveCommit(ctx, repo, row) |
| 140 | + if err != nil { |
| 141 | + ctx.Warn(0, err.Error()) |
| 142 | + return nil, nil |
| 143 | + } |
| 144 | + |
| 145 | + fIter, err := commit.Files() |
| 146 | + if err != nil { |
| 147 | + return nil, err |
| 148 | + } |
| 149 | + |
| 150 | + bg, err := NewBlameGenerator(commit, fIter) |
| 151 | + if err != nil { |
| 152 | + return nil, err |
| 153 | + } |
| 154 | + |
| 155 | + return bg, nil |
| 156 | +} |
| 157 | + |
| 158 | +func (b *Blame) resolveCommit(ctx *sql.Context, repo *gitbase.Repository, row sql.Row) (*object.Commit, error) { |
| 159 | + str, err := exprToString(ctx, b.commit, row) |
| 160 | + if err != nil { |
| 161 | + return nil, err |
| 162 | + } |
| 163 | + |
| 164 | + commitHash, err := repo.ResolveRevision(plumbing.Revision(str)) |
| 165 | + if err != nil { |
| 166 | + h := plumbing.NewHash(str) |
| 167 | + commitHash = &h |
| 168 | + } |
| 169 | + to, err := repo.CommitObject(*commitHash) |
| 170 | + if err != nil { |
| 171 | + return nil, err |
| 172 | + } |
| 173 | + |
| 174 | + return to, nil |
| 175 | +} |
| 176 | + |
| 177 | +func (b *Blame) resolveRepo(ctx *sql.Context, r sql.Row) (*gitbase.Repository, error) { |
| 178 | + repoID, err := exprToString(ctx, b.repo, r) |
| 179 | + if err != nil { |
| 180 | + return nil, err |
| 181 | + } |
| 182 | + s, ok := ctx.Session.(*gitbase.Session) |
| 183 | + if !ok { |
| 184 | + return nil, gitbase.ErrInvalidGitbaseSession.New(ctx.Session) |
| 185 | + } |
| 186 | + return s.Pool.GetRepo(repoID) |
| 187 | +} |
0 commit comments