|
| 1 | +package function |
| 2 | + |
| 3 | +import ( |
| 4 | + "io" |
| 5 | + |
| 6 | + "github.com/src-d/gitquery" |
| 7 | + git "gopkg.in/src-d/go-git.v4" |
| 8 | + "gopkg.in/src-d/go-git.v4/plumbing" |
| 9 | + "gopkg.in/src-d/go-mysql-server.v0/sql" |
| 10 | + "gopkg.in/src-d/go-mysql-server.v0/sql/expression" |
| 11 | +) |
| 12 | + |
| 13 | +// HistoryIdx is a function that returns the index of a commit in the history |
| 14 | +// of another commit. |
| 15 | +type HistoryIdx struct { |
| 16 | + expression.BinaryExpression |
| 17 | +} |
| 18 | + |
| 19 | +// NewHistoryIdx creates a new HistoryIdx udf. |
| 20 | +func NewHistoryIdx(start, target sql.Expression) sql.Expression { |
| 21 | + return &HistoryIdx{expression.BinaryExpression{Left: start, Right: target}} |
| 22 | +} |
| 23 | + |
| 24 | +// Name implements the Expression interface. |
| 25 | +func (HistoryIdx) Name() string { return "history_idx" } |
| 26 | + |
| 27 | +// Eval implements the Expression interface. |
| 28 | +func (f *HistoryIdx) Eval(session sql.Session, row sql.Row) (interface{}, error) { |
| 29 | + s, ok := session.(*gitquery.Session) |
| 30 | + if !ok { |
| 31 | + return nil, gitquery.ErrInvalidGitQuerySession.New(session) |
| 32 | + } |
| 33 | + |
| 34 | + left, err := f.Left.Eval(session, row) |
| 35 | + if err != nil { |
| 36 | + return nil, err |
| 37 | + } |
| 38 | + |
| 39 | + if left == nil { |
| 40 | + return nil, nil |
| 41 | + } |
| 42 | + |
| 43 | + left, err = sql.Text.Convert(left) |
| 44 | + if err != nil { |
| 45 | + return nil, err |
| 46 | + } |
| 47 | + |
| 48 | + right, err := f.Right.Eval(session, row) |
| 49 | + if err != nil { |
| 50 | + return nil, err |
| 51 | + } |
| 52 | + |
| 53 | + if right == nil { |
| 54 | + return nil, nil |
| 55 | + } |
| 56 | + |
| 57 | + right, err = sql.Text.Convert(right) |
| 58 | + if err != nil { |
| 59 | + return nil, err |
| 60 | + } |
| 61 | + |
| 62 | + start := plumbing.NewHash(left.(string)) |
| 63 | + target := plumbing.NewHash(right.(string)) |
| 64 | + |
| 65 | + // fast path for equal hashes |
| 66 | + if start == target { |
| 67 | + return int64(0), nil |
| 68 | + } |
| 69 | + |
| 70 | + return f.historyIdx(s.Pool, start, target) |
| 71 | +} |
| 72 | + |
| 73 | +func (f *HistoryIdx) historyIdx(pool *gitquery.RepositoryPool, start, target plumbing.Hash) (int64, error) { |
| 74 | + iter, err := pool.RepoIter() |
| 75 | + if err != nil { |
| 76 | + return 0, err |
| 77 | + } |
| 78 | + |
| 79 | + for { |
| 80 | + repo, err := iter.Next() |
| 81 | + if err == io.EOF { |
| 82 | + return -1, nil |
| 83 | + } |
| 84 | + |
| 85 | + if err != nil { |
| 86 | + return 0, err |
| 87 | + } |
| 88 | + |
| 89 | + idx, err := f.repoHistoryIdx(repo.Repo, start, target) |
| 90 | + if err != nil { |
| 91 | + return 0, err |
| 92 | + } |
| 93 | + |
| 94 | + if idx > -1 { |
| 95 | + return idx, nil |
| 96 | + } |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +type stackFrame struct { |
| 101 | + // idx from the start commit |
| 102 | + idx int64 |
| 103 | + // pos in the hashes slice |
| 104 | + pos int |
| 105 | + hashes []plumbing.Hash |
| 106 | +} |
| 107 | + |
| 108 | +func (f *HistoryIdx) repoHistoryIdx(repo *git.Repository, start, target plumbing.Hash) (int64, error) { |
| 109 | + // If the target is not on the repo we can avoid starting to traverse the |
| 110 | + // tree completely. |
| 111 | + _, err := repo.CommitObject(target) |
| 112 | + if err == plumbing.ErrObjectNotFound { |
| 113 | + return -1, nil |
| 114 | + } |
| 115 | + |
| 116 | + if err != nil { |
| 117 | + return 0, err |
| 118 | + } |
| 119 | + |
| 120 | + // Since commits can have multiple parents we cannot just do a repo.Log and |
| 121 | + // keep counting with an index how far it is, because it might go back in |
| 122 | + // the history and try another branch. |
| 123 | + // Because of that, the traversal of the history is done manually using a |
| 124 | + // stack with frames with N commit hashes, representing each level in the |
| 125 | + // history. Because the frame keeps track of which was its index, we can |
| 126 | + // return accurate indexes even if there are multiple branches. |
| 127 | + stack := []*stackFrame{{0, 0, []plumbing.Hash{start}}} |
| 128 | + |
| 129 | + for { |
| 130 | + if len(stack) == 0 { |
| 131 | + return -1, nil |
| 132 | + } |
| 133 | + |
| 134 | + frame := stack[len(stack)-1] |
| 135 | + |
| 136 | + c, err := repo.CommitObject(frame.hashes[frame.pos]) |
| 137 | + if err == plumbing.ErrObjectNotFound { |
| 138 | + return -1, nil |
| 139 | + } |
| 140 | + |
| 141 | + if err != nil { |
| 142 | + return 0, err |
| 143 | + } |
| 144 | + |
| 145 | + frame.pos++ |
| 146 | + |
| 147 | + if c.Hash == target { |
| 148 | + return frame.idx, nil |
| 149 | + } |
| 150 | + |
| 151 | + if frame.pos >= len(frame.hashes) { |
| 152 | + stack = stack[:len(stack)-1] |
| 153 | + } |
| 154 | + |
| 155 | + if c.NumParents() > 0 { |
| 156 | + stack = append(stack, &stackFrame{frame.idx + 1, 0, c.ParentHashes}) |
| 157 | + } |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +// Type implements the Expression interface. |
| 162 | +func (HistoryIdx) Type() sql.Type { return sql.Int64 } |
| 163 | + |
| 164 | +// TransformUp implements the Expression interface. |
| 165 | +func (f *HistoryIdx) TransformUp(fn func(sql.Expression) (sql.Expression, error)) (sql.Expression, error) { |
| 166 | + left, err := f.Left.TransformUp(fn) |
| 167 | + if err != nil { |
| 168 | + return nil, err |
| 169 | + } |
| 170 | + |
| 171 | + right, err := f.Right.TransformUp(fn) |
| 172 | + if err != nil { |
| 173 | + return nil, err |
| 174 | + } |
| 175 | + |
| 176 | + return fn(NewHistoryIdx(left, right)) |
| 177 | +} |
0 commit comments