diff options
Diffstat (limited to 'bug')
-rw-r--r-- | bug/bug.go | 21 | ||||
-rw-r--r-- | bug/bug_actions_test.go | 8 | ||||
-rw-r--r-- | bug/bug_test.go | 9 | ||||
-rw-r--r-- | bug/clocks.go | 4 | ||||
-rw-r--r-- | bug/interface.go | 4 | ||||
-rw-r--r-- | bug/op_set_metadata.go | 2 | ||||
-rw-r--r-- | bug/operation_iterator_test.go | 34 |
7 files changed, 51 insertions, 31 deletions
@@ -160,7 +160,7 @@ func readBug(repo repository.ClockedRepo, ref string) (*Bug, error) { rootFound = true } if strings.HasPrefix(entry.Name, createClockEntryPrefix) { - n, err := fmt.Sscanf(string(entry.Name), createClockEntryPattern, &createTime) + n, err := fmt.Sscanf(entry.Name, createClockEntryPattern, &createTime) if err != nil { return nil, errors.Wrap(err, "can't read create lamport time") } @@ -169,7 +169,7 @@ func readBug(repo repository.ClockedRepo, ref string) (*Bug, error) { } } if strings.HasPrefix(entry.Name, editClockEntryPrefix) { - n, err := fmt.Sscanf(string(entry.Name), editClockEntryPattern, &editTime) + n, err := fmt.Sscanf(entry.Name, editClockEntryPattern, &editTime) if err != nil { return nil, errors.Wrap(err, "can't read edit lamport time") } @@ -197,10 +197,10 @@ func readBug(repo repository.ClockedRepo, ref string) (*Bug, error) { } // Update the clocks - if err := repo.CreateWitness(bug.createTime); err != nil { + if err := repo.WitnessCreate(bug.createTime); err != nil { return nil, errors.Wrap(err, "failed to update create lamport clock") } - if err := repo.EditWitness(bug.editTime); err != nil { + if err := repo.WitnessEdit(bug.editTime); err != nil { return nil, errors.Wrap(err, "failed to update edit lamport clock") } @@ -330,12 +330,18 @@ func (bug *Bug) Validate() error { } // Check that there is no more CreateOp op + // Check that there is no colliding operation's ID it := NewOperationIterator(bug) createCount := 0 + ids := make(map[entity.Id]struct{}) for it.Next() { if it.Value().base().OperationType == CreateOp { createCount++ } + if _, ok := ids[it.Value().Id()]; ok { + return fmt.Errorf("id collision: %s", it.Value().Id()) + } + ids[it.Value().Id()] = struct{}{} } if createCount != 1 { @@ -350,11 +356,6 @@ func (bug *Bug) Append(op Operation) { bug.staging.Append(op) } -// HasPendingOp tell if the bug need to be committed -func (bug *Bug) HasPendingOp() bool { - return !bug.staging.IsEmpty() -} - // Commit write the staging area in Git and move the operations to the packs func (bug *Bug) Commit(repo repository.ClockedRepo) error { @@ -592,6 +593,8 @@ func (bug *Bug) Merge(repo repository.Repo, other Interface) (bool, error) { bug.lastCommit = hash } + bug.packs = newPacks + // Update the git ref err = repo.UpdateRef(bugsRefPattern+bug.id.String(), bug.lastCommit) if err != nil { diff --git a/bug/bug_actions_test.go b/bug/bug_actions_test.go index 4bc58aea..38dddce2 100644 --- a/bug/bug_actions_test.go +++ b/bug/bug_actions_test.go @@ -4,6 +4,7 @@ import ( "testing" "time" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/MichaelMure/git-bug/identity" @@ -18,8 +19,10 @@ func TestPushPull(t *testing.T) { bug1, _, err := Create(reneA, time.Now().Unix(), "bug1", "message") require.NoError(t, err) + assert.True(t, bug1.NeedCommit()) err = bug1.Commit(repoA) require.NoError(t, err) + assert.False(t, bug1.NeedCommit()) // distribute the identity _, err = identity.Push(repoA, "origin") @@ -91,8 +94,10 @@ func _RebaseTheirs(t testing.TB) { bug1, _, err := Create(reneA, time.Now().Unix(), "bug1", "message") require.NoError(t, err) + assert.True(t, bug1.NeedCommit()) err = bug1.Commit(repoA) require.NoError(t, err) + assert.False(t, bug1.NeedCommit()) // distribute the identity _, err = identity.Push(repoA, "origin") @@ -111,18 +116,21 @@ func _RebaseTheirs(t testing.TB) { bug2, err := ReadLocalBug(repoB, bug1.Id()) require.NoError(t, err) + assert.False(t, bug2.NeedCommit()) reneB, err := identity.ReadLocal(repoA, reneA.Id()) require.NoError(t, err) _, err = AddComment(bug2, reneB, time.Now().Unix(), "message2") require.NoError(t, err) + assert.True(t, bug2.NeedCommit()) _, err = AddComment(bug2, reneB, time.Now().Unix(), "message3") require.NoError(t, err) _, err = AddComment(bug2, reneB, time.Now().Unix(), "message4") require.NoError(t, err) err = bug2.Commit(repoB) require.NoError(t, err) + assert.False(t, bug2.NeedCommit()) // B --> remote _, err = Push(repoB, "origin") diff --git a/bug/bug_test.go b/bug/bug_test.go index 4e8a9440..480d312e 100644 --- a/bug/bug_test.go +++ b/bug/bug_test.go @@ -73,13 +73,14 @@ func TestBugCommitLoad(t *testing.T) { bug1.Append(createOp) bug1.Append(setTitleOp) - bug1.Append(setTitleOp) - bug1.Append(addCommentOp) repo := repository.NewMockRepoForTest() + assert.True(t, bug1.NeedCommit()) + err := bug1.Commit(repo) assert.Nil(t, err) + assert.False(t, bug1.NeedCommit()) bug2, err := ReadLocalBug(repo, bug1.Id()) assert.NoError(t, err) @@ -87,11 +88,13 @@ func TestBugCommitLoad(t *testing.T) { // add more op - bug1.Append(setTitleOp) bug1.Append(addCommentOp) + assert.True(t, bug1.NeedCommit()) + err = bug1.Commit(repo) assert.Nil(t, err) + assert.False(t, bug1.NeedCommit()) bug3, err := ReadLocalBug(repo, bug1.Id()) assert.NoError(t, err) diff --git a/bug/clocks.go b/bug/clocks.go index bb3d81f0..52d23544 100644 --- a/bug/clocks.go +++ b/bug/clocks.go @@ -12,12 +12,12 @@ func Witnesser(repo repository.ClockedRepo) error { return b.Err } - err := repo.CreateWitness(b.Bug.createTime) + err := repo.WitnessCreate(b.Bug.createTime) if err != nil { return err } - err = repo.EditWitness(b.Bug.editTime) + err = repo.WitnessEdit(b.Bug.editTime) if err != nil { return err } diff --git a/bug/interface.go b/bug/interface.go index 8266e99e..796ee569 100644 --- a/bug/interface.go +++ b/bug/interface.go @@ -16,8 +16,8 @@ type Interface interface { // Append an operation into the staging area, to be committed later Append(op Operation) - // Append an operation into the staging area, to be committed later - HasPendingOp() bool + // Indicate that the in-memory state changed and need to be commit in the repository + NeedCommit() bool // Commit write the staging area in Git and move the operations to the packs Commit(repo repository.ClockedRepo) error diff --git a/bug/op_set_metadata.go b/bug/op_set_metadata.go index f99f836b..67f7e009 100644 --- a/bug/op_set_metadata.go +++ b/bug/op_set_metadata.go @@ -34,6 +34,8 @@ func (op *SetMetadataOperation) Apply(snapshot *Snapshot) { base.extraMetadata = make(map[string]string) } + // Apply the metadata in an immutable way: if a metadata already + // exist, it's not possible to override it. for key, val := range op.NewMetadata { if _, exist := base.extraMetadata[key]; !exist { base.extraMetadata[key] = val diff --git a/bug/operation_iterator_test.go b/bug/operation_iterator_test.go index b922bec1..fcc61d0f 100644 --- a/bug/operation_iterator_test.go +++ b/bug/operation_iterator_test.go @@ -1,12 +1,14 @@ package bug import ( - "github.com/MichaelMure/git-bug/identity" - "github.com/MichaelMure/git-bug/repository" - "github.com/stretchr/testify/assert" - + "fmt" "testing" "time" + + "github.com/stretchr/testify/assert" + + "github.com/MichaelMure/git-bug/identity" + "github.com/MichaelMure/git-bug/repository" ) func ExampleOperationIterator() { @@ -29,16 +31,20 @@ func TestOpIterator(t *testing.T) { unix := time.Now().Unix() createOp := NewCreateOp(rene, unix, "title", "message", nil) - setTitleOp := NewSetTitleOp(rene, unix, "title2", "title1") addCommentOp := NewAddCommentOp(rene, unix, "message2", nil) setStatusOp := NewSetStatusOp(rene, unix, ClosedStatus) labelChangeOp := NewLabelChangeOperation(rene, unix, []Label{"added"}, []Label{"removed"}) + var i int + genTitleOp := func() Operation { + i++ + return NewSetTitleOp(rene, unix, fmt.Sprintf("title%d", i), "") + } + bug1 := NewBug() // first pack bug1.Append(createOp) - bug1.Append(setTitleOp) bug1.Append(addCommentOp) bug1.Append(setStatusOp) bug1.Append(labelChangeOp) @@ -46,16 +52,16 @@ func TestOpIterator(t *testing.T) { assert.NoError(t, err) // second pack - bug1.Append(setTitleOp) - bug1.Append(setTitleOp) - bug1.Append(setTitleOp) + bug1.Append(genTitleOp()) + bug1.Append(genTitleOp()) + bug1.Append(genTitleOp()) err = bug1.Commit(mockRepo) assert.NoError(t, err) // staging - bug1.Append(setTitleOp) - bug1.Append(setTitleOp) - bug1.Append(setTitleOp) + bug1.Append(genTitleOp()) + bug1.Append(genTitleOp()) + bug1.Append(genTitleOp()) it := NewOperationIterator(bug1) @@ -65,7 +71,5 @@ func TestOpIterator(t *testing.T) { counter++ } - if counter != 11 { - t.Fatalf("Wrong count of value iterated (%d instead of 8)", counter) - } + assert.Equal(t, 10, counter) } |