diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2022-03-24 16:30:24 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2022-03-30 12:36:43 +0300 |
commit | aebb5f0f503eacd042c78635936fb63f8e0c0dd3 (patch) | |
tree | 9bd2685929a0c1f149bdca1cbfac5c4268a03c68 /internal/git | |
parent | f67491c045d12f817389987e7f856fec28c8abef (diff) |
housekeeping: Avoid stat calls when searching for broken refs
We use `filepath.Walk()` to search for broken refs, which will stat
every single directory entry we're finding. While this cannot be avoided
for normal files, we also needlessly stat directories.
Convert the function to instead use `filepath.WalkDir()` so that we can
avoid statting directories.
Changelog: performance
Diffstat (limited to 'internal/git')
-rw-r--r-- | internal/git/housekeeping/clean_stale_data.go | 31 |
1 files changed, 20 insertions, 11 deletions
diff --git a/internal/git/housekeeping/clean_stale_data.go b/internal/git/housekeeping/clean_stale_data.go index 8d300ef48..f3b815b05 100644 --- a/internal/git/housekeeping/clean_stale_data.go +++ b/internal/git/housekeeping/clean_stale_data.go @@ -325,32 +325,41 @@ func isStaleTemporaryObject(dirEntry fs.DirEntry) (bool, error) { } func findBrokenLooseReferences(ctx context.Context, repoPath string) ([]string, error) { - logger := myLogger(ctx) - var brokenRefs []string - err := filepath.Walk(filepath.Join(repoPath, "refs"), func(path string, info os.FileInfo, err error) error { - if info == nil { - logger.WithFields(log.Fields{ - "path": path, - }).WithError(err).Error("nil FileInfo in housekeeping.Perform") + if err := filepath.WalkDir(filepath.Join(repoPath, "refs"), func(path string, dirEntry fs.DirEntry, err error) error { + if err != nil { + if errors.Is(err, fs.ErrPermission) || errors.Is(err, fs.ErrNotExist) { + return nil + } + return err + } + if dirEntry.IsDir() { return nil } + fi, err := dirEntry.Info() + if err != nil { + if errors.Is(err, fs.ErrNotExist) { + return nil + } + + return fmt.Errorf("statting loose ref: %w", err) + } + // When git crashes or a node reboots, it may happen that it leaves behind empty // references. These references break various assumptions made by git and cause it // to error in various circumstances. We thus clean them up to work around the // issue. - if info.IsDir() || info.Size() > 0 || time.Since(info.ModTime()) < brokenRefsGracePeriod { + if fi.Size() > 0 || time.Since(fi.ModTime()) < brokenRefsGracePeriod { return nil } brokenRefs = append(brokenRefs, path) return nil - }) - if err != nil { - return nil, err + }); err != nil { + return nil, fmt.Errorf("walking references: %w", err) } return brokenRefs, nil |