| 1 | package gitattr |
| 2 | |
| 3 | import ( |
| 4 | "bufio" |
| 5 | "bytes" |
| 6 | "io" |
| 7 | "strconv" |
| 8 | "strings" |
| 9 | |
| 10 | "github.com/git-lfs/git-lfs/v3/errors" |
| 11 | "github.com/git-lfs/git-lfs/v3/tr" |
| 12 | "github.com/git-lfs/wildmatch/v2" |
| 13 | ) |
| 14 | |
| 15 | const attrPrefix = "[attr]" |
| 16 | |
| 17 | type Line interface { |
| 18 | Attrs() []*Attr |
| 19 | } |
| 20 | |
| 21 | type PatternLine interface { |
| 22 | Pattern() *wildmatch.Wildmatch |
| 23 | Line |
| 24 | } |
| 25 | |
| 26 | type MacroLine interface { |
| 27 | Macro() string |
| 28 | Line |
| 29 | } |
| 30 | |
| 31 | type lineAttrs struct { |
| 32 | // Attrs is the list of attributes defined in a .gitattributes line. |
| 33 | // |
| 34 | // It is populated in-order as it was written in the .gitattributes file |
| 35 | // being read, from left to right. |
| 36 | attrs []*Attr |
| 37 | } |
| 38 | |
| 39 | func (l *lineAttrs) Attrs() []*Attr { |
| 40 | return l.attrs |
| 41 | } |
| 42 | |
| 43 | type patternLine struct { |
| 44 | // Pattern is a wildmatch pattern that, when matched, indicates that all |
| 45 | // of the below attributes (Attrs) should be applied to that tree entry. |
| 46 | // |
| 47 | // Pattern is relative to the tree in which the .gitattributes was read |
| 48 | // from. For example, /.gitattributes affects all blobs in the |
| 49 | // repository, while /path/to/.gitattributes affects all blobs that are |
| 50 | // direct or indirect children of /path/to. |
| 51 | pattern *wildmatch.Wildmatch |
| 52 | // Attrs is the list of attributes to be applied when the above pattern |
| 53 | // matches a given filename. |
| 54 | lineAttrs |
| 55 | } |
| 56 | |
| 57 | func (pl *patternLine) Pattern() *wildmatch.Wildmatch { |
| 58 | return pl.pattern |
| 59 | } |
| 60 | |
| 61 | type macroLine struct { |
| 62 | // Macro is the name of a macro that, when matched, indicates that all |
| 63 | // of the below attributes (Attrs) should be applied to that tree |
| 64 | // entry. |
| 65 | macro string |
| 66 | // Attrs is the list of attributes to be applied when the above macro |
| 67 | // name is matched for a given filename. |
| 68 | lineAttrs |
| 69 | } |
| 70 | |
| 71 | func (ml *macroLine) Macro() string { |
| 72 | return ml.macro |
| 73 | } |
| 74 | |
| 75 | // Attr is a single attribute that may be applied to a file. |
| 76 | type Attr struct { |
| 77 | // K is the name of the attribute. It is commonly, "filter", "diff", |
| 78 | // "merge", or "text". |
| 79 | // |
| 80 | // It will never contain the special "false" shorthand ("-"), or the |
| 81 | // unspecify declarative ("!"). |
| 82 | K string |
| 83 | // V is the value held by that attribute. It is commonly "lfs", or |
| 84 | // "false", indicating the special value given by a "-"-prefixed name. |
| 85 | V string |
| 86 | // Unspecified indicates whether or not this attribute was explicitly |
| 87 | // unset by prefixing the keyname with "!". |
| 88 | Unspecified bool |
| 89 | } |
| 90 | |
| 91 | // ParseLines parses the given io.Reader "r" line-wise as if it were the |
| 92 | // contents of a .gitattributes file. |
| 93 | // |
| 94 | // If an error was encountered, it will be returned and the []*Line should be |
| 95 | // considered unusable. |
| 96 | func ParseLines(r io.Reader) ([]Line, string, error) { |
| 97 | var lines []Line |
| 98 | |
| 99 | splitter := &lineEndingSplitter{} |
| 100 | |
| 101 | scanner := bufio.NewScanner(r) |
| 102 | scanner.Split(splitter.ScanLines) |
| 103 | for scanner.Scan() { |
| 104 | text := strings.TrimSpace(scanner.Text()) |
| 105 | if len(text) == 0 { |
| 106 | continue |
| 107 | } |
| 108 | |
| 109 | var pattern string |
| 110 | var applied string |
| 111 | var macro string |
| 112 | |
| 113 | switch text[0] { |
| 114 | case '#': |
| 115 | continue |
| 116 | case '"': |
| 117 | var err error |
| 118 | last := strings.LastIndex(text, "\"") |
| 119 | if last == 0 { |
| 120 | return nil, "", errors.New(tr.Tr.Get("unbalanced quote: %s", text)) |
| 121 | } |
| 122 | pattern, err = strconv.Unquote(text[:last+1]) |
| 123 | if err != nil { |
| 124 | return nil, "", errors.Wrap(err, tr.Tr.Get("unable to unquote: %s", text[:last+1])) |
| 125 | } |
| 126 | applied = strings.TrimSpace(text[last+1:]) |
| 127 | default: |
| 128 | splits := strings.SplitN(text, " ", 2) |
| 129 | |
| 130 | if strings.HasPrefix(splits[0], attrPrefix) { |
| 131 | macro = splits[0][len(attrPrefix):] |
| 132 | } else { |
| 133 | pattern = splits[0] |
| 134 | } |
| 135 | if len(splits) == 2 { |
| 136 | applied = splits[1] |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | var lineAttrs lineAttrs |
| 141 | |
| 142 | for _, s := range strings.Split(applied, " ") { |
| 143 | if s == "" { |
| 144 | continue |
| 145 | } |
| 146 | |
| 147 | var attr Attr |
| 148 | |
| 149 | if strings.HasPrefix(s, "-") { |
| 150 | attr.K = strings.TrimPrefix(s, "-") |
| 151 | attr.V = "false" |
| 152 | } else if strings.HasPrefix(s, "!") { |
| 153 | attr.K = strings.TrimPrefix(s, "!") |
| 154 | attr.Unspecified = true |
| 155 | } else if eq := strings.Index(s, "="); eq > -1 { |
| 156 | attr.K = s[:eq] |
| 157 | attr.V = s[eq+1:] |
| 158 | } else { |
| 159 | attr.K = s |
| 160 | attr.V = "true" |
| 161 | } |
| 162 | |
| 163 | lineAttrs.attrs = append(lineAttrs.attrs, &attr) |
| 164 | } |
| 165 | |
| 166 | var line Line |
| 167 | if pattern != "" { |
| 168 | matchPattern, err := newPattern(pattern) |
| 169 | if err != nil { |
| 170 | return nil, "", err |
| 171 | } |
| 172 | line = &patternLine{matchPattern, lineAttrs} |
| 173 | } else { |
| 174 | line = ¯oLine{macro, lineAttrs} |
| 175 | } |
| 176 | |
| 177 | lines = append(lines, line) |
| 178 | } |
| 179 | |
| 180 | if err := scanner.Err(); err != nil { |
| 181 | return nil, "", err |
| 182 | } |
| 183 | return lines, splitter.LineEnding(), nil |
| 184 | } |
| 185 | |
| 186 | func newPattern(pattern string) (matchPattern *wildmatch.Wildmatch, err error) { |
| 187 | defer func() { |
| 188 | if recovered := recover(); recovered != nil { |
| 189 | err = errors.New(tr.Tr.Get("invalid attribute pattern %q: %v", pattern, recovered)) |
| 190 | } |
| 191 | }() |
| 192 | |
| 193 | return wildmatch.NewWildmatch(pattern, |
| 194 | wildmatch.Basename, wildmatch.SystemCase, |
| 195 | wildmatch.GitAttributes, |
| 196 | ), nil |
| 197 | } |
| 198 | |
| 199 | // copies bufio.ScanLines(), counting LF vs CRLF in a file |
| 200 | type lineEndingSplitter struct { |
| 201 | LFCount int |
| 202 | CRLFCount int |
| 203 | } |
| 204 | |
| 205 | func (s *lineEndingSplitter) LineEnding() string { |
| 206 | if s.CRLFCount > s.LFCount { |
| 207 | return "\r\n" |
| 208 | } else if s.LFCount == 0 { |
| 209 | return "" |
| 210 | } |
| 211 | return "\n" |
| 212 | } |
| 213 | |
| 214 | func (s *lineEndingSplitter) ScanLines(data []byte, atEOF bool) (advance int, token []byte, err error) { |
| 215 | if atEOF && len(data) == 0 { |
| 216 | return 0, nil, nil |
| 217 | } |
| 218 | if i := bytes.IndexByte(data, '\n'); i >= 0 { |
| 219 | // We have a full newline-terminated line. |
| 220 | return i + 1, s.dropCR(data[0:i]), nil |
| 221 | } |
| 222 | // If we're at EOF, we have a final, non-terminated line. Return it. |
| 223 | if atEOF { |
| 224 | return len(data), data, nil |
| 225 | } |
| 226 | // Request more data. |
| 227 | return 0, nil, nil |
| 228 | } |
| 229 | |
| 230 | // dropCR drops a terminal \r from the data. |
| 231 | func (s *lineEndingSplitter) dropCR(data []byte) []byte { |
| 232 | if len(data) > 0 && data[len(data)-1] == '\r' { |
| 233 | s.CRLFCount++ |
| 234 | return data[0 : len(data)-1] |
| 235 | } |
| 236 | s.LFCount++ |
| 237 | return data |
| 238 | } |