Skip to content

Commit

Permalink
git: simplify code by using set datatype
Browse files Browse the repository at this point in the history
  • Loading branch information
fho committed Apr 16, 2024
1 parent 6151b48 commit 18c14b0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
14 changes: 5 additions & 9 deletions internal/vcs/git/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"errors"
"path/filepath"
"sync"

"github.com/simplesurance/baur/v3/internal/set"
)

const Name = "git"
Expand All @@ -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.
Expand Down Expand Up @@ -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
}

Expand All @@ -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
Expand Down
8 changes: 5 additions & 3 deletions internal/vcs/git/trackedobjects.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)

Expand All @@ -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{
Expand All @@ -83,7 +85,7 @@ func (h *TrackedObjects) createDb(ch <-chan *Object, finishedCh chan struct{}) {
continue
}

outdated[absPath] = struct{}{}
outdated.Add(absPath)
delete(m, absPath)
}

Expand Down

0 comments on commit 18c14b0

Please sign in to comment.