-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
line-based.go
91 lines (75 loc) · 2.34 KB
/
line-based.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package manifest
import (
"bufio"
"fmt"
"io"
"strings"
)
const DefaultLineBasedFetch = "refs/heads/*" // backwards compatibility
// TODO write more of a proper parser? (probably not worthwhile given that 2822 is the preferred format)
func ParseLineBasedLine(line string, defaults Manifest2822Entry) (*Manifest2822Entry, error) {
entry := defaults.Clone()
parts := strings.SplitN(line, ":", 2)
if len(parts) < 2 {
return nil, fmt.Errorf("manifest line missing ':': %s", line)
}
entry.Tags = []string{strings.TrimSpace(parts[0])}
parts = strings.SplitN(parts[1], "@", 2)
if len(parts) < 2 {
return nil, fmt.Errorf("manifest line missing '@': %s", line)
}
entry.GitRepo = strings.TrimSpace(parts[0])
parts = strings.SplitN(parts[1], " ", 2)
entry.GitCommit = strings.TrimSpace(parts[0])
if len(parts) > 1 {
entry.Directory = strings.TrimSpace(parts[1])
}
if entry.GitFetch == DefaultLineBasedFetch && !GitCommitRegex.MatchString(entry.GitCommit) {
// doesn't look like a commit, must be a tag
entry.GitFetch = "refs/tags/" + entry.GitCommit
entry.GitCommit = "FETCH_HEAD"
}
return &entry, nil
}
func ParseLineBased(readerIn io.Reader) (*Manifest2822, error) {
reader := bufio.NewReader(readerIn)
manifest := &Manifest2822{
Global: DefaultManifestEntry.Clone(),
}
manifest.Global.GitFetch = DefaultLineBasedFetch
for {
line, err := reader.ReadString('\n')
line = strings.TrimSpace(line)
if len(line) > 0 {
if line[0] == '#' {
maintainerLine := strings.TrimPrefix(line, "# maintainer: ")
if line != maintainerLine {
// if the prefix was removed, it must be a maintainer line!
manifest.Global.Maintainers = append(manifest.Global.Maintainers, maintainerLine)
}
} else {
entry, parseErr := ParseLineBasedLine(line, manifest.Global)
if parseErr != nil {
return nil, parseErr
}
err = manifest.AddEntry(*entry)
if err != nil {
return nil, err
}
}
}
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
}
if len(manifest.Global.Maintainers) < 1 {
return nil, fmt.Errorf("missing Maintainers")
}
if invalidMaintainers := manifest.Global.InvalidMaintainers(); len(invalidMaintainers) > 0 {
return nil, fmt.Errorf("invalid Maintainers: %q (expected format %q)", strings.Join(invalidMaintainers, ", "), MaintainersFormat)
}
return manifest, nil
}