line-based.go 2.3 KB

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