From 18c14b0cbdf42da08eeca9d50fc3bba25b5f8ca8 Mon Sep 17 00:00:00 2001 From: Fabian Holler Date: Tue, 16 Apr 2024 11:32:51 +0200 Subject: [PATCH] git: simplify code by using set datatype --- internal/vcs/git/repository.go | 14 +++++--------- internal/vcs/git/trackedobjects.go | 8 +++++--- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/internal/vcs/git/repository.go b/internal/vcs/git/repository.go index 818c44d4..d5003f7f 100644 --- a/internal/vcs/git/repository.go +++ b/internal/vcs/git/repository.go @@ -4,6 +4,8 @@ import ( "errors" "path/filepath" "sync" + + "github.com/simplesurance/baur/v3/internal/set" ) const Name = "git" @@ -19,7 +21,7 @@ type Repository struct { lock sync.Mutex commitID *string worktreeIsDirty *bool - untrackedFiles map[string]struct{} + untrackedFiles set.Set[string] } // NewRepositoryWithCheck returns a new Repository object for the git repository at dir. @@ -109,7 +111,7 @@ func (g *Repository) WithoutUntracked(paths ...string) ([]string, error) { relPath = p } - if _, isUntracked := g.untrackedFiles[relPath]; isUntracked { + if g.untrackedFiles.Contains(relPath) { continue } @@ -128,13 +130,7 @@ func (g *Repository) initUntrackedFiles() error { if err != nil { return err } - - m := make(map[string]struct{}, len(files)) - - for _, p := range files { - m[p] = struct{}{} - } - g.untrackedFiles = m + g.untrackedFiles = set.From(files) } return nil diff --git a/internal/vcs/git/trackedobjects.go b/internal/vcs/git/trackedobjects.go index c94f6a17..3055fe29 100644 --- a/internal/vcs/git/trackedobjects.go +++ b/internal/vcs/git/trackedobjects.go @@ -5,6 +5,8 @@ import ( "errors" "path/filepath" "sync" + + "github.com/simplesurance/baur/v3/internal/set" ) var ErrObjectNotFound = errors.New("git object id not found, file might not exist, untracked or modified") @@ -61,7 +63,7 @@ func (h *TrackedObjects) createDb(ch <-chan *Object, finishedCh chan struct{}) { const objectTypeFileOrSymlink = ObjectTypeFile | ObjectTypeSymlink m := map[string]*TrackedObject{} - outdated := map[string]struct{}{} + outdated := set.Set[string]{} defer close(finishedCh) @@ -72,7 +74,7 @@ func (h *TrackedObjects) createDb(ch <-chan *Object, finishedCh chan struct{}) { absPath := filepath.Join(h.repositoryDir, obj.RelPath) if obj.Status == ObjectStatusCached { - if _, exists := outdated[absPath]; exists { + if outdated.Contains(absPath) { continue } m[absPath] = &TrackedObject{ @@ -83,7 +85,7 @@ func (h *TrackedObjects) createDb(ch <-chan *Object, finishedCh chan struct{}) { continue } - outdated[absPath] = struct{}{} + outdated.Add(absPath) delete(m, absPath) }