Skip to content

Commit a0a8f63

Browse files
authored
Merge pull request #3730 from crazy-max/history-mv-buildname
history: move BuildName to util/history
2 parents 771e0de + b272a37 commit a0a8f63

5 files changed

Lines changed: 205 additions & 89 deletions

File tree

commands/history/inspect.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/docker/buildx/util/cobrautil/completion"
2525
"github.com/docker/buildx/util/confutil"
2626
"github.com/docker/buildx/util/desktop"
27+
historyutil "github.com/docker/buildx/util/history"
2728
"github.com/docker/cli/cli/command"
2829
"github.com/docker/cli/cli/command/formatter"
2930
"github.com/docker/cli/cli/debug"
@@ -243,7 +244,7 @@ workers0:
243244
}
244245
delete(attrs, "filename")
245246

246-
out.Name = BuildName(rec.FrontendAttrs, st)
247+
out.Name = historyutil.BuildName(rec.FrontendAttrs, st)
247248
out.Ref = rec.Ref
248249

249250
out.Context = context

commands/history/ls.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/docker/buildx/util/confutil"
1616
"github.com/docker/buildx/util/desktop"
1717
"github.com/docker/buildx/util/gitutil"
18+
historyutil "github.com/docker/buildx/util/history"
1819
"github.com/docker/cli/cli"
1920
"github.com/docker/cli/cli/command"
2021
"github.com/docker/cli/cli/command/formatter"
@@ -85,7 +86,7 @@ func runLs(ctx context.Context, dockerCli command.Cli, opts lsOptions) error {
8586

8687
for i, rec := range out {
8788
st, _ := ls.ReadRef(rec.node.Builder, rec.node.Name, rec.Ref)
88-
rec.name = BuildName(rec.FrontendAttrs, st)
89+
rec.name = historyutil.BuildName(rec.FrontendAttrs, st)
8990
out[i] = rec
9091
}
9192

commands/history/utils.go

Lines changed: 0 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,13 @@ import (
66
"encoding/csv"
77
"fmt"
88
"io"
9-
"path/filepath"
109
"slices"
1110
"strconv"
1211
"strings"
1312
"sync"
1413
"time"
1514

1615
"github.com/docker/buildx/builder"
17-
"github.com/docker/buildx/localstate"
18-
"github.com/docker/buildx/util/urlutil"
1916
"github.com/docker/cli/cli/command"
2017
controlapi "github.com/moby/buildkit/api/services/control"
2118
"github.com/moby/buildkit/frontend/dockerfile/dfgitutil"
@@ -25,90 +22,6 @@ import (
2522

2623
const recordsLimit = 50
2724

28-
func BuildName(fattrs map[string]string, ls *localstate.State) string {
29-
if v, ok := fattrs["build-arg:BUILDKIT_BUILD_NAME"]; ok && v != "" {
30-
return v
31-
}
32-
33-
var res string
34-
35-
var target, contextPath, dockerfilePath, vcsSource string
36-
if v, ok := fattrs["target"]; ok {
37-
target = v
38-
}
39-
if v, ok := fattrs["context"]; ok {
40-
contextPath = filepath.ToSlash(v)
41-
} else if v, ok := fattrs["vcs:localdir:context"]; ok && v != "." {
42-
contextPath = filepath.ToSlash(v)
43-
}
44-
if v, ok := fattrs["vcs:source"]; ok {
45-
vcsSource = v
46-
} else if v, ok := fattrs["input:context"]; ok {
47-
if _, ok, _ := dfgitutil.ParseGitRef(v); ok {
48-
vcsSource = v
49-
}
50-
}
51-
if v, ok := fattrs["filename"]; ok && v != "Dockerfile" {
52-
dockerfilePath = filepath.ToSlash(v)
53-
}
54-
if v, ok := fattrs["vcs:localdir:dockerfile"]; ok && v != "." {
55-
dockerfilePath = filepath.ToSlash(filepath.Join(v, dockerfilePath))
56-
}
57-
58-
var localPath string
59-
if ls != nil && !urlutil.IsRemoteURL(ls.LocalPath) {
60-
if ls.LocalPath != "" && ls.LocalPath != "-" {
61-
localPath = filepath.ToSlash(ls.LocalPath)
62-
}
63-
if ls.DockerfilePath != "" && ls.DockerfilePath != "-" && ls.DockerfilePath != "Dockerfile" {
64-
dockerfilePath = filepath.ToSlash(ls.DockerfilePath)
65-
}
66-
}
67-
68-
// remove default dockerfile name
69-
const defaultFilename = "/Dockerfile"
70-
hasDefaultFileName := strings.HasSuffix(dockerfilePath, defaultFilename) || dockerfilePath == ""
71-
dockerfilePath = strings.TrimSuffix(dockerfilePath, defaultFilename)
72-
73-
// dockerfile is a subpath of context
74-
if strings.HasPrefix(dockerfilePath, localPath) && len(dockerfilePath) > len(localPath) {
75-
res = dockerfilePath[strings.LastIndex(localPath, "/")+1:]
76-
} else {
77-
// Otherwise, use basename
78-
bpath := localPath
79-
if len(dockerfilePath) > 0 {
80-
bpath = dockerfilePath
81-
}
82-
if len(bpath) > 0 {
83-
lidx := strings.LastIndex(bpath, "/")
84-
res = bpath[lidx+1:]
85-
if !hasDefaultFileName {
86-
if lidx != -1 {
87-
res = filepath.ToSlash(filepath.Join(filepath.Base(bpath[:lidx]), res))
88-
} else {
89-
res = filepath.ToSlash(filepath.Join(filepath.Base(bpath), res))
90-
}
91-
}
92-
}
93-
}
94-
95-
if len(contextPath) > 0 {
96-
res = contextPath
97-
}
98-
if len(target) > 0 {
99-
if len(res) > 0 {
100-
res = res + " (" + target + ")"
101-
} else {
102-
res = target
103-
}
104-
}
105-
if res == "" && vcsSource != "" {
106-
u, _ := dfgitutil.FragmentFormat(vcsSource)
107-
return u
108-
}
109-
return res
110-
}
111-
11225
func trimBeginning(s string, n int) string {
11326
if len(s) <= n {
11427
return s

util/history/name.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package history
2+
3+
import (
4+
"path/filepath"
5+
"strings"
6+
7+
"github.com/docker/buildx/localstate"
8+
"github.com/docker/buildx/util/urlutil"
9+
"github.com/moby/buildkit/frontend/dockerfile/dfgitutil"
10+
)
11+
12+
func BuildName(fattrs map[string]string, ls *localstate.State) string {
13+
if v, ok := fattrs["build-arg:BUILDKIT_BUILD_NAME"]; ok && v != "" {
14+
return v
15+
}
16+
17+
var res string
18+
19+
var target, contextPath, dockerfilePath, vcsSource string
20+
if v, ok := fattrs["target"]; ok {
21+
target = v
22+
}
23+
if v, ok := fattrs["context"]; ok {
24+
contextPath = filepath.ToSlash(v)
25+
} else if v, ok := fattrs["vcs:localdir:context"]; ok && v != "." {
26+
contextPath = filepath.ToSlash(v)
27+
}
28+
if v, ok := fattrs["vcs:source"]; ok {
29+
vcsSource = v
30+
} else if v, ok := fattrs["input:context"]; ok {
31+
if _, ok, _ := dfgitutil.ParseGitRef(v); ok {
32+
vcsSource = v
33+
}
34+
}
35+
if v, ok := fattrs["filename"]; ok && v != "Dockerfile" {
36+
dockerfilePath = filepath.ToSlash(v)
37+
}
38+
if v, ok := fattrs["vcs:localdir:dockerfile"]; ok && v != "." {
39+
dockerfilePath = filepath.ToSlash(filepath.Join(v, dockerfilePath))
40+
}
41+
42+
var localPath string
43+
if ls != nil && !urlutil.IsRemoteURL(ls.LocalPath) {
44+
if ls.LocalPath != "" && ls.LocalPath != "-" {
45+
localPath = filepath.ToSlash(ls.LocalPath)
46+
}
47+
if ls.DockerfilePath != "" && ls.DockerfilePath != "-" && ls.DockerfilePath != "Dockerfile" {
48+
dockerfilePath = filepath.ToSlash(ls.DockerfilePath)
49+
}
50+
}
51+
52+
const defaultFilename = "/Dockerfile"
53+
hasDefaultFileName := strings.HasSuffix(dockerfilePath, defaultFilename) || dockerfilePath == ""
54+
dockerfilePath = strings.TrimSuffix(dockerfilePath, defaultFilename)
55+
56+
if strings.HasPrefix(dockerfilePath, localPath) && len(dockerfilePath) > len(localPath) {
57+
res = dockerfilePath[strings.LastIndex(localPath, "/")+1:]
58+
} else {
59+
bpath := localPath
60+
if len(dockerfilePath) > 0 {
61+
bpath = dockerfilePath
62+
}
63+
if len(bpath) > 0 {
64+
lidx := strings.LastIndex(bpath, "/")
65+
res = bpath[lidx+1:]
66+
if !hasDefaultFileName {
67+
if lidx != -1 {
68+
res = filepath.ToSlash(filepath.Join(filepath.Base(bpath[:lidx]), res))
69+
} else {
70+
res = filepath.ToSlash(filepath.Join(filepath.Base(bpath), res))
71+
}
72+
}
73+
}
74+
}
75+
76+
if len(contextPath) > 0 {
77+
res = contextPath
78+
}
79+
if len(target) > 0 {
80+
if len(res) > 0 {
81+
res = res + " (" + target + ")"
82+
} else {
83+
res = target
84+
}
85+
}
86+
if res == "" && vcsSource != "" {
87+
u, _ := dfgitutil.FragmentFormat(vcsSource)
88+
return u
89+
}
90+
return res
91+
}

util/history/name_test.go

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package history
2+
3+
import (
4+
"testing"
5+
6+
"github.com/docker/buildx/localstate"
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestBuildName(t *testing.T) {
11+
tests := []struct {
12+
name string
13+
attrs map[string]string
14+
state *localstate.State
15+
want string
16+
}{
17+
{
18+
name: "empty",
19+
want: "",
20+
},
21+
{
22+
name: "override",
23+
attrs: map[string]string{
24+
"build-arg:BUILDKIT_BUILD_NAME": "foobar",
25+
},
26+
want: "foobar",
27+
},
28+
{
29+
name: "local dockerfile path",
30+
state: &localstate.State{
31+
LocalPath: "/tmp/project",
32+
DockerfilePath: "/tmp/project/deploy/Dockerfile.release",
33+
},
34+
want: "project/deploy/Dockerfile.release",
35+
},
36+
{
37+
name: "local default dockerfile",
38+
state: &localstate.State{
39+
LocalPath: "/tmp/project",
40+
DockerfilePath: "/tmp/project/Dockerfile",
41+
},
42+
want: "project",
43+
},
44+
{
45+
name: "git query input",
46+
attrs: map[string]string{
47+
"input:context": "https://github.com/docker/buildx.git?ref=main",
48+
},
49+
want: "https://github.com/docker/buildx.git#main",
50+
},
51+
{
52+
name: "vcs source",
53+
attrs: map[string]string{
54+
"vcs:source": "https://github.com/docker/buildx.git?ref=main",
55+
},
56+
want: "https://github.com/docker/buildx.git#main",
57+
},
58+
{
59+
name: "vcs local context fallback",
60+
attrs: map[string]string{
61+
"vcs:localdir:context": "subdir",
62+
},
63+
want: "subdir",
64+
},
65+
{
66+
name: "dockerfile attrs",
67+
attrs: map[string]string{
68+
"filename": "Dockerfile.release",
69+
"vcs:localdir:dockerfile": "deploy",
70+
},
71+
want: "deploy/Dockerfile.release",
72+
},
73+
{
74+
name: "target only",
75+
attrs: map[string]string{
76+
"target": "release",
77+
},
78+
want: "release",
79+
},
80+
{
81+
name: "context overrides local name",
82+
attrs: map[string]string{
83+
"context": "subdir",
84+
"target": "release",
85+
},
86+
state: &localstate.State{
87+
LocalPath: "/tmp/project",
88+
DockerfilePath: "/tmp/project/deploy/Dockerfile.release",
89+
},
90+
want: "subdir (release)",
91+
},
92+
{
93+
name: "remote local path ignored",
94+
attrs: map[string]string{
95+
"filename": "Dockerfile.release",
96+
"vcs:localdir:dockerfile": "deploy",
97+
},
98+
state: &localstate.State{
99+
LocalPath: "https://github.com/docker/buildx.git",
100+
DockerfilePath: "/tmp/project/ignored/Dockerfile",
101+
},
102+
want: "deploy/Dockerfile.release",
103+
},
104+
}
105+
for _, tt := range tests {
106+
t.Run(tt.name, func(t *testing.T) {
107+
require.Equal(t, tt.want, BuildName(tt.attrs, tt.state))
108+
})
109+
}
110+
}

0 commit comments

Comments
 (0)