|
| 1 | +package function |
| 2 | + |
| 3 | +import ( |
| 4 | + "io" |
| 5 | + |
| 6 | + "github.com/src-d/gitquery" |
| 7 | + "gopkg.in/src-d/go-git.v4/plumbing" |
| 8 | + "gopkg.in/src-d/go-git.v4/plumbing/object" |
| 9 | + "gopkg.in/src-d/go-mysql-server.v0/sql" |
| 10 | +) |
| 11 | + |
| 12 | +// CommitContains is a function that checks whether a blob is in a commit. |
| 13 | +type CommitContains struct { |
| 14 | + commitHash sql.Expression |
| 15 | + blob sql.Expression |
| 16 | +} |
| 17 | + |
| 18 | +// NewCommitContains creates a new commit_contains function. |
| 19 | +func NewCommitContains(commitHash, blob sql.Expression) sql.Expression { |
| 20 | + return &CommitContains{ |
| 21 | + commitHash: commitHash, |
| 22 | + blob: blob, |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +// Type implements the Expression interface. |
| 27 | +func (CommitContains) Type() sql.Type { |
| 28 | + return sql.Boolean |
| 29 | +} |
| 30 | + |
| 31 | +// Eval implements the Expression interface. |
| 32 | +func (f *CommitContains) Eval(session sql.Session, row sql.Row) (interface{}, error) { |
| 33 | + s, ok := session.(*gitquery.Session) |
| 34 | + if !ok { |
| 35 | + return nil, gitquery.ErrInvalidGitQuerySession.New(session) |
| 36 | + } |
| 37 | + |
| 38 | + commitHash, err := f.commitHash.Eval(s, row) |
| 39 | + if err != nil { |
| 40 | + return nil, err |
| 41 | + } |
| 42 | + |
| 43 | + if commitHash == nil { |
| 44 | + return nil, err |
| 45 | + } |
| 46 | + |
| 47 | + commitHash, err = sql.Text.Convert(commitHash) |
| 48 | + if err != nil { |
| 49 | + return nil, err |
| 50 | + } |
| 51 | + |
| 52 | + blob, err := f.blob.Eval(s, row) |
| 53 | + if err != nil { |
| 54 | + return nil, err |
| 55 | + } |
| 56 | + |
| 57 | + if blob == nil { |
| 58 | + return nil, err |
| 59 | + } |
| 60 | + |
| 61 | + blob, err = sql.Text.Convert(blob) |
| 62 | + if err != nil { |
| 63 | + return nil, err |
| 64 | + } |
| 65 | + |
| 66 | + return f.commitContains( |
| 67 | + s.Pool, |
| 68 | + plumbing.NewHash(commitHash.(string)), |
| 69 | + plumbing.NewHash(blob.(string)), |
| 70 | + ) |
| 71 | +} |
| 72 | + |
| 73 | +func (f *CommitContains) commitContains( |
| 74 | + pool *gitquery.RepositoryPool, |
| 75 | + commitHash, blob plumbing.Hash, |
| 76 | +) (bool, error) { |
| 77 | + iter, err := pool.RepoIter() |
| 78 | + if err != nil { |
| 79 | + return false, err |
| 80 | + } |
| 81 | + |
| 82 | + for { |
| 83 | + repository, err := iter.Next() |
| 84 | + if err == io.EOF { |
| 85 | + break |
| 86 | + } |
| 87 | + |
| 88 | + if err != nil { |
| 89 | + return false, err |
| 90 | + } |
| 91 | + |
| 92 | + repo := repository.Repo |
| 93 | + commit, err := repo.CommitObject(commitHash) |
| 94 | + if err == plumbing.ErrObjectNotFound { |
| 95 | + continue |
| 96 | + } |
| 97 | + |
| 98 | + if err != nil { |
| 99 | + return false, err |
| 100 | + } |
| 101 | + |
| 102 | + tree, err := commit.Tree() |
| 103 | + if err != nil { |
| 104 | + return false, err |
| 105 | + } |
| 106 | + |
| 107 | + contained, err := hashInTree(blob, tree) |
| 108 | + if err != nil { |
| 109 | + return false, err |
| 110 | + } |
| 111 | + |
| 112 | + if contained { |
| 113 | + return true, nil |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + return false, nil |
| 118 | +} |
| 119 | + |
| 120 | +func hashInTree(hash plumbing.Hash, tree *object.Tree) (bool, error) { |
| 121 | + var contained bool |
| 122 | + err := tree.Files().ForEach(func(f *object.File) error { |
| 123 | + if f.Blob.Hash == hash { |
| 124 | + contained = true |
| 125 | + return io.EOF |
| 126 | + } |
| 127 | + return nil |
| 128 | + }) |
| 129 | + |
| 130 | + if err != nil && err != io.EOF { |
| 131 | + return false, err |
| 132 | + } |
| 133 | + |
| 134 | + return contained, nil |
| 135 | +} |
| 136 | + |
| 137 | +// Name implements the Expression interface. |
| 138 | +func (CommitContains) Name() string { |
| 139 | + return "commit_contains" |
| 140 | +} |
| 141 | + |
| 142 | +// IsNullable implements the Expression interface. |
| 143 | +func (f CommitContains) IsNullable() bool { |
| 144 | + return f.commitHash.IsNullable() || f.blob.IsNullable() |
| 145 | +} |
| 146 | + |
| 147 | +// Resolved implements the Expression interface. |
| 148 | +func (f CommitContains) Resolved() bool { |
| 149 | + return f.commitHash.Resolved() && f.blob.Resolved() |
| 150 | +} |
| 151 | + |
| 152 | +// TransformUp implements the Expression interface. |
| 153 | +func (f CommitContains) TransformUp(fn func(sql.Expression) (sql.Expression, error)) (sql.Expression, error) { |
| 154 | + commitHash, err := f.commitHash.TransformUp(fn) |
| 155 | + if err != nil { |
| 156 | + return nil, err |
| 157 | + } |
| 158 | + |
| 159 | + blob, err := f.blob.TransformUp(fn) |
| 160 | + if err != nil { |
| 161 | + return nil, err |
| 162 | + } |
| 163 | + |
| 164 | + return fn(NewCommitContains(commitHash, blob)) |
| 165 | +} |
0 commit comments