Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build: avoid possible panic when reading git info #2663

Merged
merged 1 commit into from
Aug 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions build/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,15 @@ type gitAttrsAppendFunc func(so *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 == "" {
return gitAppendNoneFunc, nil
return nil, nil
}

setGitLabels := false
Expand All @@ -42,7 +48,7 @@ func getGitAttributes(ctx context.Context, contextPath, dockerfilePath string) (
}

if !setGitLabels && !setGitInfo {
return gitAppendNoneFunc, nil
return nil, nil
}

// figure out in which directory the git command needs to run in
Expand All @@ -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() {
return nil, errors.Wrap(err, "git was not found in the system")
}
return gitAppendNoneFunc, nil
return nil, nil
}

if !gitc.IsInsideWorkTree() {
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 gitAppendNoneFunc, nil
return nil, nil
}

root, err := gitc.RootDir()
Expand Down