Skip to content

Commit

Permalink
Add Repository information to database
Browse files Browse the repository at this point in the history
  • Loading branch information
twavv committed May 3, 2023
1 parent 086fc36 commit 68f461b
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 16 deletions.
4 changes: 4 additions & 0 deletions internal/meta/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ type DB interface {
// ReadTx is a transaction that can be used to read from the database.
// It presents a consistent view of the underlying database.
type ReadTx interface {
// Repository returns the repository information.
Repository() (Repository, bool)
// Branch returns the branch with the given name. If no such branch exists,
// the second return value is false.
Branch(name string) (Branch, bool)
Expand All @@ -28,4 +30,6 @@ type WriteTx interface {
Commit() error
// SetBranch sets the given branch in the database.
SetBranch(branch Branch)
// SetRepository sets the repository information in the database.
SetRepository(repository Repository)
}
8 changes: 6 additions & 2 deletions internal/meta/jsonfiledb/readtx.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@ type readTx struct {

var _ meta.ReadTx = &readTx{}

func (tx *readTx) Repository() (meta.Repository, bool) {
return tx.state.RepositoryState, tx.state.RepositoryState.ID != ""
}

func (tx *readTx) Branch(name string) (branch meta.Branch, ok bool) {
branch, ok = tx.state.Branches[name]
branch, ok = tx.state.BranchState[name]
return
}

func (tx *readTx) AllBranches() map[string]meta.Branch {
return maputils.Copy(tx.state.Branches)
return maputils.Copy(tx.state.BranchState)
}
17 changes: 4 additions & 13 deletions internal/meta/jsonfiledb/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,14 @@ func readState(filepath string) (*state, error) {
}

type state struct {
Branches map[string]meta.Branch `json:"branches"`
}

var _ meta.ReadTx = &state{}

func (d *state) Branch(name string) (meta.Branch, bool) {
branch, ok := d.Branches[name]
return branch, ok
}

func (d *state) AllBranches() map[string]meta.Branch {
return maputils.Copy(d.Branches)
BranchState map[string]meta.Branch `json:"branches"`
RepositoryState meta.Repository `json:"repository"`
}

func (d *state) copy() state {
return state{
maputils.Copy(d.Branches),
maputils.Copy(d.BranchState),
d.RepositoryState,
}
}

Expand Down
6 changes: 5 additions & 1 deletion internal/meta/jsonfiledb/writetx.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ type writeTx struct {
readTx
}

func (tx *writeTx) SetRepository(repository meta.Repository) {
tx.state.RepositoryState = repository
}

func (tx *writeTx) SetBranch(branch meta.Branch) {
tx.state.Branches[branch.Name] = branch
tx.state.BranchState[branch.Name] = branch
}

func (tx *writeTx) Abort() {
Expand Down

0 comments on commit 68f461b

Please sign in to comment.