Merge pull request #45 from infosiftr/single-line

Finally deprecate the legacy line-based format
This commit is contained in:
yosifkit 2022-06-08 17:20:59 -07:00 committed by GitHub
commit b4880ac5f1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 6 additions and 166 deletions

View File

@ -209,13 +209,8 @@ func (r Repo) fetchGitRepo(arch string, entry *manifest.Manifest2822Entry) (stri
}
defer os.RemoveAll(tempRefDir)
tempRef := path.Join(refBase, filepath.Base(tempRefDir))
if entry.ArchGitFetch(arch) == manifest.DefaultLineBasedFetch {
// backwards compat (see manifest/line-based.go in go-dockerlibrary)
fetchStrings[0] += tempRef + "/*"
} else {
fetchStrings[0] += tempRef + "/temp"
}
tempRef := path.Join(refBase, filepath.Base(tempRefDir)) + "/temp"
fetchStrings[0] += tempRef
fetchStrings = append([]string{
// Git (and more recently, GitHub) support "git fetch"ing a specific commit directly!
@ -223,7 +218,7 @@ func (r Repo) fetchGitRepo(arch string, entry *manifest.Manifest2822Entry) (stri
// https://github.com/git/git/commit/f8edeaa05d8623a9f6dad408237496c51101aad8
// https://github.com/go-git/go-git/pull/58
// If that works, we want to prefer it (since it'll be much more efficient at getting us the commit we care about), so we prepend it to our list of "things to try fetching"
entryArchGitCommit + ":" + tempRef + "/temp",
entryArchGitCommit + ":" + tempRef,
}, fetchStrings...)
}

View File

@ -83,23 +83,6 @@ s390x-File: Dockerfile
}
fmt.Printf("\n")
man, err = manifest.Parse(bufio.NewReader(strings.NewReader(`
# maintainer: InfoSiftr <github@infosiftr.com> (@infosiftr)
# maintainer: John Smith <jsmith@example.com> (@example-jsmith)
# first set
a: b@c d
e: b@c d
# second set
f: g@h
i: g@h j
`)))
if err != nil {
panic(err)
}
fmt.Printf("-------------\nline-based:\n%v\n", man)
// Output:
// -------------
// 2822:
@ -145,27 +128,6 @@ i: g@h j
//
// - raspbian
// - raspbian-s390x
//
// -------------
// line-based:
// Maintainers: InfoSiftr <github@infosiftr.com> (@infosiftr), John Smith <jsmith@example.com> (@example-jsmith)
// GitFetch: refs/heads/*
//
// Tags: a, e
// GitRepo: b
// GitCommit: c
// Directory: d
//
// Tags: f
// GitRepo: g
// GitFetch: refs/tags/h
// GitCommit: FETCH_HEAD
//
// Tags: i
// GitRepo: g
// GitFetch: refs/tags/h
// GitCommit: FETCH_HEAD
// Directory: j
}
func ExampleFetch_local() {

View File

@ -1,95 +0,0 @@
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!
maintainer := Manifest2822Maintainer{}
if err := maintainer.UnmarshalControl(maintainerLine); err != nil {
return nil, err
}
manifest.Global.Maintainers = append(manifest.Global.Maintainers, maintainer)
}
} 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
}

View File

@ -1,25 +0,0 @@
package manifest
import (
"bytes"
"io"
)
// try parsing as a 2822 manifest, but fallback to line-based if that fails
func Parse(reader io.Reader) (*Manifest2822, error) {
buf := &bytes.Buffer{}
// try parsing as 2822, but also copy back into a new buffer so that if it fails, we can re-parse as line-based
manifest, err2822 := Parse2822(io.TeeReader(reader, buf))
if err2822 != nil {
manifest, err := ParseLineBased(buf)
if err != nil {
// if we fail parsing line-based, eat the error and return the 2822 parsing error instead
// https://github.com/docker-library/bashbrew/issues/16
return nil, err2822
}
return manifest, nil
}
return manifest, nil
}

View File

@ -581,6 +581,9 @@ func (decoder *decoderWrapper) Decode(entry *Manifest2822Entry) error {
}
}
// https://github.com/docker-library/bashbrew/issues/16
var Parse = Parse2822
func Parse2822(readerIn io.Reader) (*Manifest2822, error) {
reader := stripper.NewCommentStripper(readerIn)