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

🐛 save file names during gitfile handler setup to avoid data race #4522

Merged
merged 1 commit into from
Feb 16, 2025
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
39 changes: 29 additions & 10 deletions internal/gitfile/gitfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
gitRepo *git.Repository
tempDir string
commitSHA string
files []string
}

func (h *Handler) Init(ctx context.Context, cloneURL, commitSHA string) {
Expand All @@ -51,6 +52,7 @@
h.ctx = ctx
h.cloneURL = cloneURL
h.commitSHA = commitSHA
h.files = nil
}

func (h *Handler) setup() error {
Expand Down Expand Up @@ -85,6 +87,15 @@
return
}
}

// go-git is not thread-safe so list the files inside this sync.Once and save them
// https://github.com/go-git/go-git/issues/773
files, err := enumerateFiles(h.gitRepo)
if err != nil {
h.errSetup = err
return
}

Check warning on line 97 in internal/gitfile/gitfile.go

View check run for this annotation

Codecov / codecov/patch

internal/gitfile/gitfile.go#L95-L97

Added lines #L95 - L97 were not covered by tests
h.files = files
})
return h.errSetup
}
Expand All @@ -100,12 +111,27 @@
if err := h.setup(); err != nil {
return nil, fmt.Errorf("setup: %w", err)
}
ref, err := h.gitRepo.Head()
var files []string
for _, f := range h.files {
shouldInclude, err := predicate(f)
if err != nil {
return nil, fmt.Errorf("error applying predicate to file %s: %w", f, err)
}

Check warning on line 119 in internal/gitfile/gitfile.go

View check run for this annotation

Codecov / codecov/patch

internal/gitfile/gitfile.go#L118-L119

Added lines #L118 - L119 were not covered by tests

if shouldInclude {
files = append(files, f)
}
}
return files, nil
}

func enumerateFiles(repo *git.Repository) ([]string, error) {
ref, err := repo.Head()
if err != nil {
return nil, fmt.Errorf("git.Head: %w", err)
}

commit, err := h.gitRepo.CommitObject(ref.Hash())
commit, err := repo.CommitObject(ref.Hash())
if err != nil {
return nil, fmt.Errorf("git.CommitObject: %w", err)
}
Expand All @@ -117,14 +143,7 @@

var files []string
err = tree.Files().ForEach(func(f *object.File) error {
shouldInclude, err := predicate(f.Name)
if err != nil {
return fmt.Errorf("error applying predicate to file %s: %w", f.Name, err)
}

if shouldInclude {
files = append(files, f.Name)
}
files = append(files, f.Name)
return nil
})
if err != nil {
Expand Down
Loading