From c7d3d0d9357883c4e23b92df1e17a2891bb9ee96 Mon Sep 17 00:00:00 2001 From: Tianon Gravi Date: Mon, 30 Jan 2023 14:01:10 -0800 Subject: [PATCH 1/2] Add "bashbrew fetch" command In many, many, many scripts I have encoded the horrible assumption that `ArchDockerFroms` will ensure our Git repository is fetched (which is a side-effect of that function). With the implementation of `Builder: oci-import`, that side effect is not necessary for returning an accurate result in these new images. Instead of working around this in bad ways, I've decided to finally bite the bullet and add an explicit `bashbrew fetch` command that can make sure all the underlying Git commits are fully fetched into the local cache. --- cmd/bashbrew/cmd-fetch.go | 51 +++++++++++++++++++++++++++++++++++++++ cmd/bashbrew/main.go | 13 ++++++++++ 2 files changed, 64 insertions(+) create mode 100644 cmd/bashbrew/cmd-fetch.go diff --git a/cmd/bashbrew/cmd-fetch.go b/cmd/bashbrew/cmd-fetch.go new file mode 100644 index 00000000..a14d39d7 --- /dev/null +++ b/cmd/bashbrew/cmd-fetch.go @@ -0,0 +1,51 @@ +package main + +import ( + "fmt" + "os" + + "github.com/urfave/cli" +) + +func cmdFetch(c *cli.Context) error { + repos, err := repos(c.Bool("all"), c.Args()...) + if err != nil { + return cli.NewMultiError(fmt.Errorf(`failed gathering repo list`), err) + } + + applyConstraints := c.Bool("apply-constraints") + archFilter := c.Bool("arch-filter") + + for _, repo := range repos { + r, err := fetch(repo) + if err != nil { + return cli.NewMultiError(fmt.Errorf(`failed fetching repo %q`, repo), err) + } + + for _, entry := range r.Entries() { + if applyConstraints && r.SkipConstraints(entry) { + continue + } + if archFilter && !entry.HasArchitecture(arch) { + continue + } + + arches := entry.Architectures + if applyConstraints || archFilter { + arches = []string{arch} + } + + for _, entryArch := range arches { + commit, err := r.fetchGitRepo(entryArch, entry) + if err != nil { + return cli.NewMultiError(fmt.Errorf(`failed fetching git repo for %q (tags %q on arch %q)`, r.RepoName, entry.TagsString(), entryArch), err) + } + if debugFlag { + fmt.Fprintf(os.Stderr, "DEBUG: fetched %s (%q, %q)\n", commit, r.EntryIdentifier(entry), entryArch) + } + } + } + } + + return nil +} diff --git a/cmd/bashbrew/main.go b/cmd/bashbrew/main.go index efc20ff6..75385460 100644 --- a/cmd/bashbrew/main.go +++ b/cmd/bashbrew/main.go @@ -402,6 +402,19 @@ func main() { Category: "plumbing", }, + { + Name: "fetch", + Usage: "ensure Git contents are local", + Flags: []cli.Flag{ + commonFlags["all"], + commonFlags["apply-constraints"], + commonFlags["arch-filter"], + }, + Before: subcommandBeforeFactory("fetch"), + Action: cmdFetch, + + Category: "plumbing", + }, { Name: "remote", Usage: "query registries for bashbrew-related data", From cec429fa5330b084feeaf973aa4e69582b5b87a5 Mon Sep 17 00:00:00 2001 From: Tianon Gravi Date: Mon, 30 Jan 2023 14:25:51 -0800 Subject: [PATCH 2/2] Add "gitCache" template function This not only returns the path to the "git cache" directory, but ensures it is initialized and that `git` commands against it will generally succeed. --- cmd/bashbrew/cmd-cat.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/cmd/bashbrew/cmd-cat.go b/cmd/bashbrew/cmd-cat.go index 01daae40..c2fac1c0 100644 --- a/cmd/bashbrew/cmd-cat.go +++ b/cmd/bashbrew/cmd-cat.go @@ -56,6 +56,13 @@ func cmdCat(c *cli.Context) error { "arch": func() string { return arch }, + "gitCache": func() (string, error) { + err := ensureGitInit() + if err != nil { + return "", err + } + return gitCache(), nil + }, "ociPlatform": func(arch string) *architecture.OCIPlatform { if ociArch, ok := architecture.SupportedArches[arch]; ok { return &ociArch