Skip to content

Commit 7331897

Browse files
committed
git url: support specifying checksum in query
Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
1 parent 0d50a8b commit 7331897

7 files changed

Lines changed: 23 additions & 5 deletions

File tree

client/llb/source.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,9 @@ func Git(url, ref string, opts ...GitOption) State {
323323
}
324324

325325
checksum := gi.Checksum
326+
if checksum == "" && remote != nil && remote.Opts != nil {
327+
checksum = remote.Opts.Checksum
328+
}
326329
if checksum != "" {
327330
attrs[pb.AttrGitChecksum] = checksum
328331
addCap(&gi.Constraints, pb.CapSourceGitChecksum)

frontend/dockerfile/dfgitutil/git_ref.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ type GitRef struct {
2727
// Commit is optional.
2828
Commit string
2929

30+
// Checksum verifies the Commit if specified.
31+
// Checksum is optional.
32+
Checksum string
33+
3034
// SubDir is a directory path inside the repo.
3135
// SubDir is optional.
3236
SubDir string
@@ -99,7 +103,7 @@ func ParseGitRef(ref string) (*GitRef, error) {
99103
_, res.Remote, _ = strings.Cut(res.Remote, "://")
100104
}
101105
if remote.Opts != nil {
102-
res.Commit, res.SubDir = remote.Opts.Ref, remote.Opts.Subdir
106+
res.Commit, res.Checksum, res.SubDir = remote.Opts.Ref, remote.Opts.Checksum, remote.Opts.Subdir
103107
}
104108

105109
repoSplitBySlash := strings.Split(res.Remote, "/")

frontend/dockerfile/dockerfile2llb/convert.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1517,11 +1517,14 @@ func dispatchCopy(d *dispatchState, cfg copyConfig) error {
15171517
if gitRef.SubDir != "" {
15181518
commit += ":" + gitRef.SubDir
15191519
}
1520-
gitOptions := []llb.GitOption{llb.WithCustomName(pgName)}
1520+
gitOptions := []llb.GitOption{llb.WithCustomName(pgName), llb.GitChecksum(gitRef.Checksum)}
15211521
if cfg.keepGitDir {
15221522
gitOptions = append(gitOptions, llb.KeepGitDir())
15231523
}
15241524
if cfg.checksum != "" {
1525+
if gitRef.Checksum != "" && gitRef.Checksum != cfg.checksum {
1526+
return errors.Errorf("conflicting checksum: %s vs %s", gitRef.Checksum, cfg.checksum)
1527+
}
15251528
gitOptions = append(gitOptions, llb.GitChecksum(cfg.checksum))
15261529
}
15271530
st := llb.Git(gitRef.Remote, commit, gitOptions...)

frontend/dockerui/context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ func DetectGitContext(ref string, keepGit bool) (*llb.State, bool) {
149149
if g.SubDir != "" {
150150
commit += ":" + g.SubDir
151151
}
152-
gitOpts := []llb.GitOption{WithInternalName("load git source " + ref)}
152+
gitOpts := []llb.GitOption{WithInternalName("load git source " + ref), llb.GitChecksum(g.Checksum)}
153153
if keepGit {
154154
gitOpts = append(gitOpts, llb.KeepGitDir())
155155
}

source/git/identifier.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ func NewGitIdentifier(remoteURL string) (*GitIdentifier, error) {
3434
repo := GitIdentifier{Remote: u.Remote}
3535
if u.Opts != nil {
3636
repo.Ref = u.Opts.Ref
37+
repo.Checksum = u.Opts.Checksum
3738
repo.Subdir = u.Opts.Subdir
3839
}
3940
if sd := path.Clean(repo.Subdir); sd == "/" || sd == "." {

util/gitutil/git_url.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ type GitURL struct {
6060
type GitURLOpts struct {
6161
// Ref is the git reference
6262
Ref string
63+
// Checksum is the commit hash
64+
Checksum string
6365
// Subdir is the sub-directory inside the git repository to use
6466
Subdir string
6567
}
@@ -97,6 +99,8 @@ func parseOpts(fragment string, query url.Values) (*GitURLOpts, error) {
9799
tag = v[0]
98100
case "branch":
99101
branch = v[0]
102+
case "checksum", "commit":
103+
opts.Checksum = v[0]
100104
case "subdir":
101105
if opts.Subdir != "" && opts.Subdir != v[0] {
102106
return nil, errors.Errorf("subdir conflicts: %q vs %q", opts.Subdir, v[0])
@@ -122,6 +126,9 @@ func parseOpts(fragment string, query url.Values) (*GitURLOpts, error) {
122126
}
123127
opts.Ref = "refs/heads/" + branch
124128
}
129+
if opts.Checksum != "" && opts.Ref == "" {
130+
opts.Ref = opts.Checksum
131+
}
125132
return opts, nil
126133
}
127134

util/gitutil/git_url_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,12 @@ func TestParseURL(t *testing.T) {
170170
},
171171
},
172172
{
173-
url: "https://github.com/moby/buildkit?tag=v1.0.0",
173+
url: "https://github.com/moby/buildkit?tag=v1.0.0&commit=deadbeef",
174174
result: GitURL{
175175
Scheme: HTTPSProtocol,
176176
Host: "github.com",
177177
Path: "/moby/buildkit",
178-
Opts: &GitURLOpts{Ref: "refs/tags/v1.0.0"},
178+
Opts: &GitURLOpts{Ref: "refs/tags/v1.0.0", Checksum: "deadbeef"},
179179
},
180180
},
181181
{

0 commit comments

Comments
 (0)