| 126 | } |
| 127 | |
| 128 | func LookupBranchByName(ctx context.Context, api Interface, poolName, branchName string) (*db.BranchMeta, error) { |
| 129 | b := newBuffer(db.BranchMeta{}) |
| 130 | query := fmt.Sprintf("from :branches | pool.name == '%s' branch.name == '%s'", poolName, branchName) |
| 131 | q, err := api.Query(ctx, srcfiles.Plain(query)) |
| 132 | if err != nil { |
| 133 | return nil, err |
| 134 | } |
| 135 | defer q.Pull(true) |
| 136 | if err := vio.Copy(b, q); err != nil { |
| 137 | return nil, err |
| 138 | } |
| 139 | switch len(b.results) { |
| 140 | case 0: |
| 141 | return nil, fmt.Errorf("%q: branch not found", poolName+"/"+branchName) |
| 142 | case 1: |
| 143 | branch, ok := b.results[0].(*db.BranchMeta) |
| 144 | if !ok { |
| 145 | return nil, fmt.Errorf("internal error: branch record has wrong type: %T", b.results[0]) |
| 146 | } |
| 147 | return branch, nil |
| 148 | default: |
| 149 | return nil, fmt.Errorf("internal error: multiple branches found with same name: %s", poolName+"/"+branchName) |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | func LookupBranchByID(ctx context.Context, api Interface, id ksuid.KSUID) (*db.BranchMeta, error) { |
| 154 | b := newBuffer(db.BranchMeta{}) |