diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2023-01-27 14:52:40 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2023-01-27 15:36:33 +0300 |
commit | 3c5d792a702f14b020944aa55c5b6a5da9d6bf2a (patch) | |
tree | 21da58a1cb6b0ae6dabd11c5820e85313fd1ff67 | |
parent | 85db02d15d45a143c58ab6b1c7dedfeebeb24dbd (diff) |
git/stats: Refactor nested switch statements
We've got some nested switch statements when reading packfile info for a
repository that are comparatively hard to read. Refactor them so that we
only have a single top-level switch statement for all cases.
-rw-r--r-- | internal/git/stats/repository_info.go | 32 |
1 files changed, 17 insertions, 15 deletions
diff --git a/internal/git/stats/repository_info.go b/internal/git/stats/repository_info.go index 87e7fb8b9..7ce5f2106 100644 --- a/internal/git/stats/repository_info.go +++ b/internal/git/stats/repository_info.go @@ -285,28 +285,26 @@ func PackfilesInfoForRepository(repo *localrepo.Repo) (PackfilesInfo, error) { entryName := entry.Name() switch { + case hasPrefixAndSuffix(entryName, "pack-", ".pack"): + info.Count++ + if entryInfo.Size() > 0 { + info.Size += uint64(entryInfo.Size()) + } + case hasPrefixAndSuffix(entryName, "pack-", ".bitmap"): + bitmap, err := BitmapInfoForPath(filepath.Join(packfilesPath, entryName)) + if err != nil { + return PackfilesInfo{}, fmt.Errorf("reading bitmap info: %w", err) + } + + info.Bitmap = bitmap case strings.HasPrefix(entryName, "pack-"): // We're overly lenient here and only verify packfiles for known suffixes. // As a consequence, we don't catch garbage files here. This is on purpose // though because Git has grown more and more metadata-style file formats, // and we don't want to copy the list here. - switch { - case strings.HasSuffix(entryName, ".pack"): - info.Count++ - if entryInfo.Size() > 0 { - info.Size += uint64(entryInfo.Size()) - } - case strings.HasSuffix(entryName, ".bitmap"): - bitmap, err := BitmapInfoForPath(filepath.Join(packfilesPath, entryName)) - if err != nil { - return PackfilesInfo{}, fmt.Errorf("reading bitmap info: %w", err) - } - - info.Bitmap = bitmap - } case entryName == "multi-pack-index": info.HasMultiPackIndex = true - case strings.HasPrefix(entryName, "multi-pack-index-") && strings.HasSuffix(entryName, ".bitmap"): + case hasPrefixAndSuffix(entryName, "multi-pack-index-", ".bitmap"): bitmap, err := BitmapInfoForPath(filepath.Join(packfilesPath, entryName)) if err != nil { return PackfilesInfo{}, fmt.Errorf("reading multi-pack-index bitmap info: %w", err) @@ -326,6 +324,10 @@ func PackfilesInfoForRepository(repo *localrepo.Repo) (PackfilesInfo, error) { return info, nil } +func hasPrefixAndSuffix(s, prefix, suffix string) bool { + return strings.HasPrefix(s, prefix) && strings.HasSuffix(s, suffix) +} + func readAlternates(repo *localrepo.Repo) ([]string, error) { repoPath, err := repo.Path() if err != nil { |