Skip to content

Commit bc4037d

Browse files
7ACtianon
authored andcommitted
bashbrew: account for namespaces when sorting repos
1 parent 19e63bb commit bc4037d

File tree

4 files changed

+23
-12
lines changed

4 files changed

+23
-12
lines changed

go/src/bashbrew/cmd-build.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ func cmdBuild(c *cli.Context) error {
1212
return cli.NewMultiError(fmt.Errorf(`failed gathering repo list`), err)
1313
}
1414

15-
repos, err = sortRepos(repos, true)
15+
namespace := c.String("namespace")
16+
repos, err = sortRepos(repos, true, namespace)
1617
if err != nil {
1718
return cli.NewMultiError(fmt.Errorf(`failed sorting repo list`), err)
1819
}
1920

2021
uniq := c.Bool("uniq")
21-
namespace := c.String("namespace")
2222
pull := c.String("pull")
2323
switch pull {
2424
case "always", "missing", "never":
@@ -34,7 +34,7 @@ func cmdBuild(c *cli.Context) error {
3434
return cli.NewMultiError(fmt.Errorf(`failed fetching repo %q`, repo), err)
3535
}
3636

37-
entries, err := r.SortedEntries(true)
37+
entries, err := r.SortedEntries(true, namespace)
3838
if err != nil {
3939
return cli.NewMultiError(fmt.Errorf(`failed sorting entries list for %q`, repo), err)
4040
}

go/src/bashbrew/cmd-list.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ func cmdList(c *cli.Context) error {
1414
}
1515

1616
uniq := c.Bool("uniq")
17-
namespace := ""
17+
namespace := c.String("namespace")
1818
applyConstraints := c.Bool("apply-constraints")
1919
onlyRepos := c.Bool("repos")
2020

2121
buildOrder := c.Bool("build-order")
2222
if buildOrder {
23-
repos, err = sortRepos(repos, applyConstraints)
23+
repos, err = sortRepos(repos, applyConstraints, namespace)
2424
if err != nil {
2525
return cli.NewMultiError(fmt.Errorf(`failed sorting repo list`), err)
2626
}
@@ -45,7 +45,7 @@ func cmdList(c *cli.Context) error {
4545

4646
var entries []*manifest.Manifest2822Entry
4747
if buildOrder {
48-
entries, err = r.SortedEntries(applyConstraints)
48+
entries, err = r.SortedEntries(applyConstraints, namespace)
4949
if err != nil {
5050
return cli.NewMultiError(fmt.Errorf(`failed sorting entries list for %q`, repo), err)
5151
}

go/src/bashbrew/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ func main() {
220220
Flags: []cli.Flag{
221221
commonFlags["all"],
222222
commonFlags["uniq"],
223+
commonFlags["namespace"],
223224
commonFlags["apply-constraints"],
224225
cli.BoolFlag{
225226
Name: "build-order",

go/src/bashbrew/sort.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"pault.ag/go/topsort"
66
)
77

8-
func sortRepos(repos []string, applyConstraints bool) ([]string, error) {
8+
func sortRepos(repos []string, applyConstraints bool, namespace string) ([]string, error) {
99
rs := []*Repo{}
1010
rsMap := map[*Repo]string{}
1111
for _, repo := range repos {
@@ -26,7 +26,7 @@ func sortRepos(repos []string, applyConstraints bool) ([]string, error) {
2626
return repos, nil
2727
}
2828

29-
rs, err := sortRepoObjects(rs, applyConstraints)
29+
rs, err := sortRepoObjects(rs, applyConstraints, namespace)
3030
if err != nil {
3131
return nil, err
3232
}
@@ -38,7 +38,7 @@ func sortRepos(repos []string, applyConstraints bool) ([]string, error) {
3838
return ret, nil
3939
}
4040

41-
func (r Repo) SortedEntries(applyConstraints bool) ([]*manifest.Manifest2822Entry, error) {
41+
func (r Repo) SortedEntries(applyConstraints bool, namespace string) ([]*manifest.Manifest2822Entry, error) {
4242
entries := r.Entries()
4343

4444
// short circuit if we don't have to go further
@@ -52,7 +52,7 @@ func (r Repo) SortedEntries(applyConstraints bool) ([]*manifest.Manifest2822Entr
5252
rs = append(rs, r.EntryRepo(entries[i]))
5353
}
5454

55-
rs, err := sortRepoObjects(rs, applyConstraints)
55+
rs, err := sortRepoObjects(rs, applyConstraints, namespace)
5656
if err != nil {
5757
return nil, err
5858
}
@@ -64,7 +64,14 @@ func (r Repo) SortedEntries(applyConstraints bool) ([]*manifest.Manifest2822Entr
6464
return ret, nil
6565
}
6666

67-
func sortRepoObjects(rs []*Repo, applyConstraints bool) ([]*Repo, error) {
67+
func addNamespace(namespace, tag string) string {
68+
if namespace != "" {
69+
tag = namespace + "/" + tag
70+
}
71+
return tag
72+
}
73+
74+
func sortRepoObjects(rs []*Repo, applyConstraints bool, namespace string) ([]*Repo, error) {
6875
// short circuit if we don't have to go further
6976
if noSortFlag || len(rs) <= 1 {
7077
return rs, nil
@@ -79,12 +86,13 @@ func sortRepoObjects(rs []*Repo, applyConstraints bool) ([]*Repo, error) {
7986
for _, r := range rs {
8087
node := r.Identifier()
8188
for _, entry := range r.Entries() {
89+
node = addNamespace(namespace, node)
8290
for _, tag := range r.Tags("", false, entry) {
91+
tag = addNamespace(namespace, tag)
8392
if canonicalRepo, ok := canonicalRepos[tag]; ok && canonicalRepo.TagName != "" {
8493
// if we run into a duplicate, we want to prefer a specific tag over a full repo
8594
continue
8695
}
87-
8896
canonicalNodes[tag] = node
8997
canonicalRepos[tag] = r
9098
}
@@ -115,6 +123,8 @@ func sortRepoObjects(rs []*Repo, applyConstraints bool) ([]*Repo, error) {
115123

116124
// TODO somehow reconcile/avoid "a:a -> b:b, b:b -> a:c" (which will exhibit here as cyclic)
117125
for _, tag := range r.Tags("", false, entry) {
126+
tag = addNamespace(namespace, tag)
127+
118128
if tagNode, ok := canonicalNodes[tag]; ok {
119129
if tagNode == fromNode {
120130
// don't be cyclic

0 commit comments

Comments
 (0)