mirror of
https://github.com/docker/buildx.git
synced 2024-11-22 15:37:16 +08:00
build: avoid possible panic when reading git info
Not all the error cases from getGitAttributes returned appendNoneFunc. When nil was returned it caused a panic. Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
This commit is contained in:
parent
b6a2c96926
commit
d1d8d6e19c
16
build/git.go
16
build/git.go
@ -21,9 +21,15 @@ type gitAttrsAppendFunc func(so *client.SolveOpt)
|
|||||||
|
|
||||||
func gitAppendNoneFunc(_ *client.SolveOpt) {}
|
func gitAppendNoneFunc(_ *client.SolveOpt) {}
|
||||||
|
|
||||||
func getGitAttributes(ctx context.Context, contextPath, dockerfilePath string) (gitAttrsAppendFunc, error) {
|
func getGitAttributes(ctx context.Context, contextPath, dockerfilePath string) (f gitAttrsAppendFunc, err error) {
|
||||||
|
defer func() {
|
||||||
|
if f == nil {
|
||||||
|
f = gitAppendNoneFunc
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
if contextPath == "" {
|
if contextPath == "" {
|
||||||
return gitAppendNoneFunc, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
setGitLabels := false
|
setGitLabels := false
|
||||||
@ -42,7 +48,7 @@ func getGitAttributes(ctx context.Context, contextPath, dockerfilePath string) (
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !setGitLabels && !setGitInfo {
|
if !setGitLabels && !setGitInfo {
|
||||||
return gitAppendNoneFunc, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// figure out in which directory the git command needs to run in
|
// figure out in which directory the git command needs to run in
|
||||||
@ -59,14 +65,14 @@ func getGitAttributes(ctx context.Context, contextPath, dockerfilePath string) (
|
|||||||
if st, err1 := os.Stat(path.Join(wd, ".git")); err1 == nil && st.IsDir() {
|
if st, err1 := os.Stat(path.Join(wd, ".git")); err1 == nil && st.IsDir() {
|
||||||
return nil, errors.Wrap(err, "git was not found in the system")
|
return nil, errors.Wrap(err, "git was not found in the system")
|
||||||
}
|
}
|
||||||
return gitAppendNoneFunc, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if !gitc.IsInsideWorkTree() {
|
if !gitc.IsInsideWorkTree() {
|
||||||
if st, err := os.Stat(path.Join(wd, ".git")); err == nil && st.IsDir() {
|
if st, err := os.Stat(path.Join(wd, ".git")); err == nil && st.IsDir() {
|
||||||
return nil, errors.New("failed to read current commit information with git rev-parse --is-inside-work-tree")
|
return nil, errors.New("failed to read current commit information with git rev-parse --is-inside-work-tree")
|
||||||
}
|
}
|
||||||
return gitAppendNoneFunc, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
root, err := gitc.RootDir()
|
root, err := gitc.RootDir()
|
||||||
|
Loading…
Reference in New Issue
Block a user