|
| 1 | +// Copyright 2022 The Gitea Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a MIT-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package foreachref |
| 6 | + |
| 7 | +import ( |
| 8 | + "bufio" |
| 9 | + "bytes" |
| 10 | + "fmt" |
| 11 | + "io" |
| 12 | + "strings" |
| 13 | +) |
| 14 | + |
| 15 | +// Parser parses 'git for-each-ref' output according to a given output Format. |
| 16 | +type Parser struct { |
| 17 | + // tokenizes 'git for-each-ref' output into "reference paragraphs". |
| 18 | + scanner *bufio.Scanner |
| 19 | + |
| 20 | + // format represents the '--format' string that describes the expected |
| 21 | + // 'git for-each-ref' output structure. |
| 22 | + format Format |
| 23 | + |
| 24 | + // err holds the last encountered error during parsing. |
| 25 | + err error |
| 26 | +} |
| 27 | + |
| 28 | +// NewParser creates a 'git for-each-ref' output parser that will parse all |
| 29 | +// references in the provided Reader. The references in the output are assumed |
| 30 | +// to follow the specified Format. |
| 31 | +func NewParser(r io.Reader, format Format) *Parser { |
| 32 | + scanner := bufio.NewScanner(r) |
| 33 | + |
| 34 | + // in addition to the reference delimiter we specified in the --format, |
| 35 | + // `git for-each-ref` will always add a newline after every reference. |
| 36 | + refDelim := make([]byte, 0, len(format.refDelim)+1) |
| 37 | + refDelim = append(refDelim, format.refDelim...) |
| 38 | + refDelim = append(refDelim, '\n') |
| 39 | + |
| 40 | + // Split input into delimiter-separated "reference blocks". |
| 41 | + scanner.Split( |
| 42 | + func(data []byte, atEOF bool) (advance int, token []byte, err error) { |
| 43 | + // Scan until delimiter, marking end of reference. |
| 44 | + delimIdx := bytes.Index(data, refDelim) |
| 45 | + if delimIdx >= 0 { |
| 46 | + token := data[:delimIdx] |
| 47 | + advance := delimIdx + len(refDelim) |
| 48 | + return advance, token, nil |
| 49 | + } |
| 50 | + // If we're at EOF, we have a final, non-terminated reference. Return it. |
| 51 | + if atEOF { |
| 52 | + return len(data), data, nil |
| 53 | + } |
| 54 | + // Not yet a full field. Request more data. |
| 55 | + return 0, nil, nil |
| 56 | + }) |
| 57 | + |
| 58 | + return &Parser{ |
| 59 | + scanner: scanner, |
| 60 | + format: format, |
| 61 | + err: nil, |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +// Next returns the next reference as a collection of key-value pairs. nil |
| 66 | +// denotes EOF but is also returned on errors. The Err method should always be |
| 67 | +// consulted after Next returning nil. |
| 68 | +// |
| 69 | +// It could, for example return something like: |
| 70 | +// |
| 71 | +// { "objecttype": "tag", "refname:short": "v1.16.4", "object": "f460b7543ed500e49c133c2cd85c8c55ee9dbe27" } |
| 72 | +// |
| 73 | +func (p *Parser) Next() map[string]string { |
| 74 | + if !p.scanner.Scan() { |
| 75 | + return nil |
| 76 | + } |
| 77 | + fields, err := p.parseRef(p.scanner.Text()) |
| 78 | + if err != nil { |
| 79 | + p.err = err |
| 80 | + return nil |
| 81 | + } |
| 82 | + return fields |
| 83 | +} |
| 84 | + |
| 85 | +// Err returns the latest encountered parsing error. |
| 86 | +func (p *Parser) Err() error { |
| 87 | + return p.err |
| 88 | +} |
| 89 | + |
| 90 | +// parseRef parses out all key-value pairs from a single reference block, such as |
| 91 | +// |
| 92 | +// "objecttype tag\0refname:short v1.16.4\0object f460b7543ed500e49c133c2cd85c8c55ee9dbe27" |
| 93 | +// |
| 94 | +func (p *Parser) parseRef(refBlock string) (map[string]string, error) { |
| 95 | + if refBlock == "" { |
| 96 | + // must be at EOF |
| 97 | + return nil, nil |
| 98 | + } |
| 99 | + |
| 100 | + fieldValues := make(map[string]string) |
| 101 | + |
| 102 | + fields := strings.Split(refBlock, p.format.fieldDelimStr) |
| 103 | + if len(fields) != len(p.format.fieldNames) { |
| 104 | + return nil, fmt.Errorf("unexpected number of reference fields: wanted %d, was %d", |
| 105 | + len(fields), len(p.format.fieldNames)) |
| 106 | + } |
| 107 | + for i, field := range fields { |
| 108 | + field = strings.TrimSpace(field) |
| 109 | + |
| 110 | + var fieldKey string |
| 111 | + var fieldVal string |
| 112 | + firstSpace := strings.Index(field, " ") |
| 113 | + if firstSpace > 0 { |
| 114 | + fieldKey = field[:firstSpace] |
| 115 | + fieldVal = field[firstSpace+1:] |
| 116 | + } else { |
| 117 | + // could be the case if the requested field had no value |
| 118 | + fieldKey = field |
| 119 | + } |
| 120 | + |
| 121 | + // enforce the format order of fields |
| 122 | + if p.format.fieldNames[i] != fieldKey { |
| 123 | + return nil, fmt.Errorf("unexpected field name at position %d: wanted: '%s', was: '%s'", |
| 124 | + i, p.format.fieldNames[i], fieldKey) |
| 125 | + } |
| 126 | + |
| 127 | + fieldValues[fieldKey] = fieldVal |
| 128 | + } |
| 129 | + |
| 130 | + return fieldValues, nil |
| 131 | +} |
0 commit comments