aboutsummaryrefslogtreecommitdiffstats
path: root/bug
diff options
context:
space:
mode:
Diffstat (limited to 'bug')
-rw-r--r--bug/bug.go19
-rw-r--r--bug/bug_actions.go46
-rw-r--r--bug/bug_actions_test.go259
-rw-r--r--bug/bug_test.go14
-rw-r--r--bug/op_create_test.go3
-rw-r--r--bug/op_edit_comment_test.go12
-rw-r--r--bug/op_set_metadata_test.go12
-rw-r--r--bug/operation_iterator_test.go28
-rw-r--r--bug/operation_pack.go2
-rw-r--r--bug/operation_pack_test.go25
-rw-r--r--bug/operation_test.go11
11 files changed, 274 insertions, 157 deletions
diff --git a/bug/bug.go b/bug/bug.go
index f84753fa..f1bd1114 100644
--- a/bug/bug.go
+++ b/bug/bug.go
@@ -321,6 +321,11 @@ func (bug *Bug) Validate() error {
return fmt.Errorf("first operation should be a Create op")
}
+ // The bug ID should be the hash of the first commit
+ if len(bug.packs) > 0 && string(bug.packs[0].commitHash) != bug.id {
+ return fmt.Errorf("bug id should be the first commit hash")
+ }
+
// Check that there is no more CreateOp op
it := NewOperationIterator(bug)
createCount := 0
@@ -349,7 +354,8 @@ func (bug *Bug) HasPendingOp() bool {
// Commit write the staging area in Git and move the operations to the packs
func (bug *Bug) Commit(repo repository.ClockedRepo) error {
- if bug.staging.IsEmpty() {
+
+ if !bug.NeedCommit() {
return fmt.Errorf("can't commit a bug with no pending operation")
}
@@ -466,6 +472,17 @@ func (bug *Bug) Commit(repo repository.ClockedRepo) error {
return nil
}
+func (bug *Bug) CommitAsNeeded(repo repository.ClockedRepo) error {
+ if !bug.NeedCommit() {
+ return nil
+ }
+ return bug.Commit(repo)
+}
+
+func (bug *Bug) NeedCommit() bool {
+ return !bug.staging.IsEmpty()
+}
+
func makeMediaTree(pack OperationPack) []repository.TreeEntry {
var tree []repository.TreeEntry
counter := 0
diff --git a/bug/bug_actions.go b/bug/bug_actions.go
index f214716d..b906b938 100644
--- a/bug/bug_actions.go
+++ b/bug/bug_actions.go
@@ -4,28 +4,68 @@ import (
"fmt"
"strings"
+ "github.com/MichaelMure/git-bug/identity"
"github.com/MichaelMure/git-bug/repository"
"github.com/pkg/errors"
)
+// Note:
+//
+// For the actions (fetch/push/pull/merge), this package act as a master for
+// the identity package and will also drive the needed identity actions. That is,
+// if bug.Push() is called, identity.Push will also be called to make sure that
+// the dependant identities are also present and up to date on the remote.
+//
+// I'm not entirely sure this is the correct way to do it, as it might introduce
+// too much complexity and hard coupling, but it does make this package easier
+// to use.
+
// Fetch retrieve updates from a remote
// This does not change the local bugs state
func Fetch(repo repository.Repo, remote string) (string, error) {
+ stdout, err := identity.Fetch(repo, remote)
+ if err != nil {
+ return stdout, err
+ }
+
remoteRefSpec := fmt.Sprintf(bugsRemoteRefPattern, remote)
fetchRefSpec := fmt.Sprintf("%s*:%s*", bugsRefPattern, remoteRefSpec)
- return repo.FetchRefs(remote, fetchRefSpec)
+ stdout2, err := repo.FetchRefs(remote, fetchRefSpec)
+
+ return stdout + "\n" + stdout2, err
}
// Push update a remote with the local changes
func Push(repo repository.Repo, remote string) (string, error) {
- return repo.PushRefs(remote, bugsRefPattern+"*")
+ stdout, err := identity.Push(repo, remote)
+ if err != nil {
+ return stdout, err
+ }
+
+ stdout2, err := repo.PushRefs(remote, bugsRefPattern+"*")
+
+ return stdout + "\n" + stdout2, err
}
// Pull will do a Fetch + MergeAll
// This function will return an error if a merge fail
func Pull(repo repository.ClockedRepo, remote string) error {
- _, err := Fetch(repo, remote)
+ _, err := identity.Fetch(repo, remote)
+ if err != nil {
+ return err
+ }
+
+ for merge := range identity.MergeAll(repo, remote) {
+ if merge.Err != nil {
+ return merge.Err
+ }
+ if merge.Status == identity.MergeStatusInvalid {
+ return errors.Errorf("merge failure: %s", merge.Reason)
+ }
+ }
+
+ _, err = Fetch(repo, remote)
if err != nil {
return err
}
diff --git a/bug/bug_actions_test.go b/bug/bug_actions_test.go
index 39438ec7..4d42fb1d 100644
--- a/bug/bug_actions_test.go
+++ b/bug/bug_actions_test.go
@@ -1,8 +1,11 @@
package bug
import (
+ "time"
+
+ "github.com/MichaelMure/git-bug/identity"
"github.com/MichaelMure/git-bug/util/test"
- "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
"testing"
)
@@ -11,20 +14,19 @@ func TestPushPull(t *testing.T) {
repoA, repoB, remote := test.SetupReposAndRemote(t)
defer test.CleanupRepos(repoA, repoB, remote)
- err := rene.Commit(repoA)
- assert.NoError(t, err)
+ reneA := identity.NewIdentity("René Descartes", "rene@descartes.fr")
- bug1, _, err := Create(rene, unix, "bug1", "message")
- assert.NoError(t, err)
+ bug1, _, err := Create(reneA, time.Now().Unix(), "bug1", "message")
+ require.NoError(t, err)
err = bug1.Commit(repoA)
- assert.NoError(t, err)
+ require.NoError(t, err)
// A --> remote --> B
_, err = Push(repoA, "origin")
- assert.NoError(t, err)
+ require.NoError(t, err)
err = Pull(repoB, "origin")
- assert.NoError(t, err)
+ require.NoError(t, err)
bugs := allBugs(t, ReadAllLocalBugs(repoB))
@@ -33,16 +35,19 @@ func TestPushPull(t *testing.T) {
}
// B --> remote --> A
- bug2, _, err := Create(rene, unix, "bug2", "message")
- assert.NoError(t, err)
+ reneB, err := identity.ReadLocal(repoA, reneA.Id())
+ require.NoError(t, err)
+
+ bug2, _, err := Create(reneB, time.Now().Unix(), "bug2", "message")
+ require.NoError(t, err)
err = bug2.Commit(repoB)
- assert.NoError(t, err)
+ require.NoError(t, err)
_, err = Push(repoB, "origin")
- assert.NoError(t, err)
+ require.NoError(t, err)
err = Pull(repoA, "origin")
- assert.NoError(t, err)
+ require.NoError(t, err)
bugs = allBugs(t, ReadAllLocalBugs(repoA))
@@ -76,38 +81,43 @@ func _RebaseTheirs(t testing.TB) {
repoA, repoB, remote := test.SetupReposAndRemote(t)
defer test.CleanupRepos(repoA, repoB, remote)
- bug1, _, err := Create(rene, unix, "bug1", "message")
- assert.NoError(t, err)
+ reneA := identity.NewIdentity("René Descartes", "rene@descartes.fr")
+
+ bug1, _, err := Create(reneA, time.Now().Unix(), "bug1", "message")
+ require.NoError(t, err)
err = bug1.Commit(repoA)
- assert.NoError(t, err)
+ require.NoError(t, err)
// A --> remote
_, err = Push(repoA, "origin")
- assert.NoError(t, err)
+ require.NoError(t, err)
// remote --> B
err = Pull(repoB, "origin")
- assert.NoError(t, err)
+ require.NoError(t, err)
bug2, err := ReadLocalBug(repoB, bug1.Id())
- assert.NoError(t, err)
-
- _, err = AddComment(bug2, rene, unix, "message2")
- assert.NoError(t, err)
- _, err = AddComment(bug2, rene, unix, "message3")
- assert.NoError(t, err)
- _, err = AddComment(bug2, rene, unix, "message4")
- assert.NoError(t, err)
+ require.NoError(t, err)
+
+ reneB, err := identity.ReadLocal(repoA, reneA.Id())
+ require.NoError(t, err)
+
+ _, err = AddComment(bug2, reneB, time.Now().Unix(), "message2")
+ require.NoError(t, err)
+ _, 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)
- assert.NoError(t, err)
+ require.NoError(t, err)
// B --> remote
_, err = Push(repoB, "origin")
- assert.NoError(t, err)
+ require.NoError(t, err)
// remote --> A
err = Pull(repoA, "origin")
- assert.NoError(t, err)
+ require.NoError(t, err)
bugs := allBugs(t, ReadAllLocalBugs(repoB))
@@ -116,7 +126,7 @@ func _RebaseTheirs(t testing.TB) {
}
bug3, err := ReadLocalBug(repoA, bug1.Id())
- assert.NoError(t, err)
+ require.NoError(t, err)
if nbOps(bug3) != 4 {
t.Fatal("Unexpected number of operations")
@@ -137,49 +147,51 @@ func _RebaseOurs(t testing.TB) {
repoA, repoB, remote := test.SetupReposAndRemote(t)
defer test.CleanupRepos(repoA, repoB, remote)
- bug1, _, err := Create(rene, unix, "bug1", "message")
- assert.NoError(t, err)
+ reneA := identity.NewIdentity("René Descartes", "rene@descartes.fr")
+
+ bug1, _, err := Create(reneA, time.Now().Unix(), "bug1", "message")
+ require.NoError(t, err)
err = bug1.Commit(repoA)
- assert.NoError(t, err)
+ require.NoError(t, err)
// A --> remote
_, err = Push(repoA, "origin")
- assert.NoError(t, err)
+ require.NoError(t, err)
// remote --> B
err = Pull(repoB, "origin")
- assert.NoError(t, err)
-
- _, err = AddComment(bug1, rene, unix, "message2")
- assert.NoError(t, err)
- _, err = AddComment(bug1, rene, unix, "message3")
- assert.NoError(t, err)
- _, err = AddComment(bug1, rene, unix, "message4")
- assert.NoError(t, err)
+ require.NoError(t, err)
+
+ _, err = AddComment(bug1, reneA, time.Now().Unix(), "message2")
+ require.NoError(t, err)
+ _, err = AddComment(bug1, reneA, time.Now().Unix(), "message3")
+ require.NoError(t, err)
+ _, err = AddComment(bug1, reneA, time.Now().Unix(), "message4")
+ require.NoError(t, err)
err = bug1.Commit(repoA)
- assert.NoError(t, err)
-
- _, err = AddComment(bug1, rene, unix, "message5")
- assert.NoError(t, err)
- _, err = AddComment(bug1, rene, unix, "message6")
- assert.NoError(t, err)
- _, err = AddComment(bug1, rene, unix, "message7")
- assert.NoError(t, err)
+ require.NoError(t, err)
+
+ _, err = AddComment(bug1, reneA, time.Now().Unix(), "message5")
+ require.NoError(t, err)
+ _, err = AddComment(bug1, reneA, time.Now().Unix(), "message6")
+ require.NoError(t, err)
+ _, err = AddComment(bug1, reneA, time.Now().Unix(), "message7")
+ require.NoError(t, err)
err = bug1.Commit(repoA)
- assert.NoError(t, err)
-
- _, err = AddComment(bug1, rene, unix, "message8")
- assert.NoError(t, err)
- _, err = AddComment(bug1, rene, unix, "message9")
- assert.NoError(t, err)
- _, err = AddComment(bug1, rene, unix, "message10")
- assert.NoError(t, err)
+ require.NoError(t, err)
+
+ _, err = AddComment(bug1, reneA, time.Now().Unix(), "message8")
+ require.NoError(t, err)
+ _, err = AddComment(bug1, reneA, time.Now().Unix(), "message9")
+ require.NoError(t, err)
+ _, err = AddComment(bug1, reneA, time.Now().Unix(), "message10")
+ require.NoError(t, err)
err = bug1.Commit(repoA)
- assert.NoError(t, err)
+ require.NoError(t, err)
// remote --> A
err = Pull(repoA, "origin")
- assert.NoError(t, err)
+ require.NoError(t, err)
bugs := allBugs(t, ReadAllLocalBugs(repoA))
@@ -188,7 +200,7 @@ func _RebaseOurs(t testing.TB) {
}
bug2, err := ReadLocalBug(repoA, bug1.Id())
- assert.NoError(t, err)
+ require.NoError(t, err)
if nbOps(bug2) != 10 {
t.Fatal("Unexpected number of operations")
@@ -218,83 +230,88 @@ func _RebaseConflict(t testing.TB) {
repoA, repoB, remote := test.SetupReposAndRemote(t)
defer test.CleanupRepos(repoA, repoB, remote)
- bug1, _, err := Create(rene, unix, "bug1", "message")
- assert.NoError(t, err)
+ reneA := identity.NewIdentity("René Descartes", "rene@descartes.fr")
+
+ bug1, _, err := Create(reneA, time.Now().Unix(), "bug1", "message")
+ require.NoError(t, err)
err = bug1.Commit(repoA)
- assert.NoError(t, err)
+ require.NoError(t, err)
// A --> remote
_, err = Push(repoA, "origin")
- assert.NoError(t, err)
+ require.NoError(t, err)
// remote --> B
err = Pull(repoB, "origin")
- assert.NoError(t, err)
-
- _, err = AddComment(bug1, rene, unix, "message2")
- assert.NoError(t, err)
- _, err = AddComment(bug1, rene, unix, "message3")
- assert.NoError(t, err)
- _, err = AddComment(bug1, rene, unix, "message4")
- assert.NoError(t, err)
+ require.NoError(t, err)
+
+ _, err = AddComment(bug1, reneA, time.Now().Unix(), "message2")
+ require.NoError(t, err)
+ _, err = AddComment(bug1, reneA, time.Now().Unix(), "message3")
+ require.NoError(t, err)
+ _, err = AddComment(bug1, reneA, time.Now().Unix(), "message4")
+ require.NoError(t, err)
err = bug1.Commit(repoA)
- assert.NoError(t, err)
-
- _, err = AddComment(bug1, rene, unix, "message5")
- assert.NoError(t, err)
- _, err = AddComment(bug1, rene, unix, "message6")
- assert.NoError(t, err)
- _, err = AddComment(bug1, rene, unix, "message7")
- assert.NoError(t, err)
+ require.NoError(t, err)
+
+ _, err = AddComment(bug1, reneA, time.Now().Unix(), "message5")
+ require.NoError(t, err)
+ _, err = AddComment(bug1, reneA, time.Now().Unix(), "message6")
+ require.NoError(t, err)
+ _, err = AddComment(bug1, reneA, time.Now().Unix(), "message7")
+ require.NoError(t, err)
err = bug1.Commit(repoA)
- assert.NoError(t, err)
-
- _, err = AddComment(bug1, rene, unix, "message8")
- assert.NoError(t, err)
- _, err = AddComment(bug1, rene, unix, "message9")
- assert.NoError(t, err)
- _, err = AddComment(bug1, rene, unix, "message10")
- assert.NoError(t, err)
+ require.NoError(t, err)
+
+ _, err = AddComment(bug1, reneA, time.Now().Unix(), "message8")
+ require.NoError(t, err)
+ _, err = AddComment(bug1, reneA, time.Now().Unix(), "message9")
+ require.NoError(t, err)
+ _, err = AddComment(bug1, reneA, time.Now().Unix(), "message10")
+ require.NoError(t, err)
err = bug1.Commit(repoA)
- assert.NoError(t, err)
+ require.NoError(t, err)
bug2, err := ReadLocalBug(repoB, bug1.Id())
- assert.NoError(t, err)
-
- _, err = AddComment(bug2, rene, unix, "message11")
- assert.NoError(t, err)
- _, err = AddComment(bug2, rene, unix, "message12")
- assert.NoError(t, err)
- _, err = AddComment(bug2, rene, unix, "message13")
- assert.NoError(t, err)
+ require.NoError(t, err)
+
+ reneB, err := identity.ReadLocal(repoA, reneA.Id())
+ require.NoError(t, err)
+
+ _, err = AddComment(bug2, reneB, time.Now().Unix(), "message11")
+ require.NoError(t, err)
+ _, err = AddComment(bug2, reneB, time.Now().Unix(), "message12")
+ require.NoError(t, err)
+ _, err = AddComment(bug2, reneB, time.Now().Unix(), "message13")
+ require.NoError(t, err)
err = bug2.Commit(repoB)
- assert.NoError(t, err)
-
- _, err = AddComment(bug2, rene, unix, "message14")
- assert.NoError(t, err)
- _, err = AddComment(bug2, rene, unix, "message15")
- assert.NoError(t, err)
- _, err = AddComment(bug2, rene, unix, "message16")
- assert.NoError(t, err)
+ require.NoError(t, err)
+
+ _, err = AddComment(bug2, reneB, time.Now().Unix(), "message14")
+ require.NoError(t, err)
+ _, err = AddComment(bug2, reneB, time.Now().Unix(), "message15")
+ require.NoError(t, err)
+ _, err = AddComment(bug2, reneB, time.Now().Unix(), "message16")
+ require.NoError(t, err)
err = bug2.Commit(repoB)
- assert.NoError(t, err)
-
- _, err = AddComment(bug2, rene, unix, "message17")
- assert.NoError(t, err)
- _, err = AddComment(bug2, rene, unix, "message18")
- assert.NoError(t, err)
- _, err = AddComment(bug2, rene, unix, "message19")
- assert.NoError(t, err)
+ require.NoError(t, err)
+
+ _, err = AddComment(bug2, reneB, time.Now().Unix(), "message17")
+ require.NoError(t, err)
+ _, err = AddComment(bug2, reneB, time.Now().Unix(), "message18")
+ require.NoError(t, err)
+ _, err = AddComment(bug2, reneB, time.Now().Unix(), "message19")
+ require.NoError(t, err)
err = bug2.Commit(repoB)
- assert.NoError(t, err)
+ require.NoError(t, err)
// A --> remote
_, err = Push(repoA, "origin")
- assert.NoError(t, err)
+ require.NoError(t, err)
// remote --> B
err = Pull(repoB, "origin")
- assert.NoError(t, err)
+ require.NoError(t, err)
bugs := allBugs(t, ReadAllLocalBugs(repoB))
@@ -303,7 +320,7 @@ func _RebaseConflict(t testing.TB) {
}
bug3, err := ReadLocalBug(repoB, bug1.Id())
- assert.NoError(t, err)
+ require.NoError(t, err)
if nbOps(bug3) != 19 {
t.Fatal("Unexpected number of operations")
@@ -311,11 +328,11 @@ func _RebaseConflict(t testing.TB) {
// B --> remote
_, err = Push(repoB, "origin")
- assert.NoError(t, err)
+ require.NoError(t, err)
// remote --> A
err = Pull(repoA, "origin")
- assert.NoError(t, err)
+ require.NoError(t, err)
bugs = allBugs(t, ReadAllLocalBugs(repoA))
@@ -324,7 +341,7 @@ func _RebaseConflict(t testing.TB) {
}
bug4, err := ReadLocalBug(repoA, bug1.Id())
- assert.NoError(t, err)
+ require.NoError(t, err)
if nbOps(bug4) != 19 {
t.Fatal("Unexpected number of operations")
diff --git a/bug/bug_test.go b/bug/bug_test.go
index 001bfc56..e104f921 100644
--- a/bug/bug_test.go
+++ b/bug/bug_test.go
@@ -1,6 +1,9 @@
package bug
import (
+ "time"
+
+ "github.com/MichaelMure/git-bug/identity"
"github.com/MichaelMure/git-bug/repository"
"github.com/stretchr/testify/assert"
@@ -12,6 +15,9 @@ func TestBugId(t *testing.T) {
bug1 := NewBug()
+ rene := identity.NewIdentity("René Descartes", "rene@descartes.fr")
+ createOp := NewCreateOp(rene, time.Now().Unix(), "title", "message", nil)
+
bug1.Append(createOp)
err := bug1.Commit(mockRepo)
@@ -28,6 +34,9 @@ func TestBugValidity(t *testing.T) {
bug1 := NewBug()
+ rene := identity.NewIdentity("René Descartes", "rene@descartes.fr")
+ createOp := NewCreateOp(rene, time.Now().Unix(), "title", "message", nil)
+
if bug1.Validate() == nil {
t.Fatal("Empty bug should be invalid")
}
@@ -58,6 +67,11 @@ func TestBugValidity(t *testing.T) {
func TestBugCommitLoad(t *testing.T) {
bug1 := NewBug()
+ rene := identity.NewIdentity("René Descartes", "rene@descartes.fr")
+ createOp := NewCreateOp(rene, time.Now().Unix(), "title", "message", nil)
+ setTitleOp := NewSetTitleOp(rene, time.Now().Unix(), "title2", "title1")
+ addCommentOp := NewAddCommentOp(rene, time.Now().Unix(), "message2", nil)
+
bug1.Append(createOp)
bug1.Append(setTitleOp)
bug1.Append(setTitleOp)
diff --git a/bug/op_create_test.go b/bug/op_create_test.go
index 065b81c5..c41c5687 100644
--- a/bug/op_create_test.go
+++ b/bug/op_create_test.go
@@ -12,8 +12,7 @@ import (
func TestCreate(t *testing.T) {
snapshot := Snapshot{}
- var rene = identity.NewIdentity("René Descartes", "rene@descartes.fr")
-
+ rene := identity.NewBare("René Descartes", "rene@descartes.fr")
unix := time.Now().Unix()
create := NewCreateOp(rene, unix, "title", "message", nil)
diff --git a/bug/op_edit_comment_test.go b/bug/op_edit_comment_test.go
index dbdf341d..72f8a168 100644
--- a/bug/op_edit_comment_test.go
+++ b/bug/op_edit_comment_test.go
@@ -7,30 +7,26 @@ import (
"github.com/MichaelMure/git-bug/identity"
"github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
)
func TestEdit(t *testing.T) {
snapshot := Snapshot{}
- var rene = identity.NewIdentity("René Descartes", "rene@descartes.fr")
-
+ rene := identity.NewBare("René Descartes", "rene@descartes.fr")
unix := time.Now().Unix()
create := NewCreateOp(rene, unix, "title", "create", nil)
create.Apply(&snapshot)
hash1, err := create.Hash()
- if err != nil {
- t.Fatal(err)
- }
+ require.NoError(t, err)
comment := NewAddCommentOp(rene, unix, "comment", nil)
comment.Apply(&snapshot)
hash2, err := comment.Hash()
- if err != nil {
- t.Fatal(err)
- }
+ require.NoError(t, err)
edit := NewEditCommentOp(rene, unix, hash1, "create edited", nil)
edit.Apply(&snapshot)
diff --git a/bug/op_set_metadata_test.go b/bug/op_set_metadata_test.go
index 847164f3..a7f9f313 100644
--- a/bug/op_set_metadata_test.go
+++ b/bug/op_set_metadata_test.go
@@ -7,13 +7,13 @@ import (
"github.com/MichaelMure/git-bug/identity"
"github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
)
func TestSetMetadata(t *testing.T) {
snapshot := Snapshot{}
- var rene = identity.NewIdentity("René Descartes", "rene@descartes.fr")
-
+ rene := identity.NewBare("René Descartes", "rene@descartes.fr")
unix := time.Now().Unix()
create := NewCreateOp(rene, unix, "title", "create", nil)
@@ -22,9 +22,7 @@ func TestSetMetadata(t *testing.T) {
snapshot.Operations = append(snapshot.Operations, create)
hash1, err := create.Hash()
- if err != nil {
- t.Fatal(err)
- }
+ require.NoError(t, err)
comment := NewAddCommentOp(rene, unix, "comment", nil)
comment.SetMetadata("key2", "value2")
@@ -32,9 +30,7 @@ func TestSetMetadata(t *testing.T) {
snapshot.Operations = append(snapshot.Operations, comment)
hash2, err := comment.Hash()
- if err != nil {
- t.Fatal(err)
- }
+ require.NoError(t, err)
op1 := NewSetMetadataOp(rene, unix, hash1, map[string]string{
"key": "override",
diff --git a/bug/operation_iterator_test.go b/bug/operation_iterator_test.go
index a41120e2..2865d25d 100644
--- a/bug/operation_iterator_test.go
+++ b/bug/operation_iterator_test.go
@@ -10,14 +10,17 @@ import (
)
var (
- rene = identity.NewIdentity("René Descartes", "rene@descartes.fr")
- 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"})
+// Beware, don't those test data in multi-repo situation !
+// As an example, the Identity would be considered commited after a commit
+// in one repo,
+// rene = identity.NewIdentity("René Descartes", "rene@descartes.fr")
+// 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"})
)
func ExampleOperationIterator() {
@@ -36,6 +39,15 @@ func ExampleOperationIterator() {
func TestOpIterator(t *testing.T) {
mockRepo := repository.NewMockRepoForTest()
+ rene := identity.NewIdentity("René Descartes", "rene@descartes.fr")
+ 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"})
+
bug1 := NewBug()
// first pack
diff --git a/bug/operation_pack.go b/bug/operation_pack.go
index 1ffc1d1a..55fc018e 100644
--- a/bug/operation_pack.go
+++ b/bug/operation_pack.go
@@ -147,7 +147,7 @@ func (opp *OperationPack) Write(repo repository.Repo) (git.Hash, error) {
// First, make sure that all the identities are properly Commit as well
for _, op := range opp.Operations {
- err := op.base().Author.Commit(repo)
+ err := op.base().Author.CommitAsNeeded(repo)
if err != nil {
return "", err
}
diff --git a/bug/operation_pack_test.go b/bug/operation_pack_test.go
index 8a8c7e62..09d159af 100644
--- a/bug/operation_pack_test.go
+++ b/bug/operation_pack_test.go
@@ -3,27 +3,37 @@ package bug
import (
"encoding/json"
"testing"
+ "time"
+ "github.com/MichaelMure/git-bug/identity"
"github.com/MichaelMure/git-bug/util/git"
"github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
)
func TestOperationPackSerialize(t *testing.T) {
opp := &OperationPack{}
+ rene := identity.NewBare("René Descartes", "rene@descartes.fr")
+ createOp := NewCreateOp(rene, time.Now().Unix(), "title", "message", nil)
+ setTitleOp := NewSetTitleOp(rene, time.Now().Unix(), "title2", "title1")
+ addCommentOp := NewAddCommentOp(rene, time.Now().Unix(), "message2", nil)
+ setStatusOp := NewSetStatusOp(rene, time.Now().Unix(), ClosedStatus)
+ labelChangeOp := NewLabelChangeOperation(rene, time.Now().Unix(), []Label{"added"}, []Label{"removed"})
+
opp.Append(createOp)
opp.Append(setTitleOp)
opp.Append(addCommentOp)
opp.Append(setStatusOp)
opp.Append(labelChangeOp)
- opMeta := NewCreateOp(rene, unix, "title", "message", nil)
+ opMeta := NewSetTitleOp(rene, time.Now().Unix(), "title3", "title2")
opMeta.SetMetadata("key", "value")
opp.Append(opMeta)
assert.Equal(t, 1, len(opMeta.Metadata))
- opFile := NewCreateOp(rene, unix, "title", "message", []git.Hash{
+ opFile := NewAddCommentOp(rene, time.Now().Unix(), "message", []git.Hash{
"abcdef",
"ghijkl",
})
@@ -36,7 +46,16 @@ func TestOperationPackSerialize(t *testing.T) {
var opp2 *OperationPack
err = json.Unmarshal(data, &opp2)
-
assert.NoError(t, err)
+
+ ensureHash(t, opp)
+
assert.Equal(t, opp, opp2)
}
+
+func ensureHash(t *testing.T, opp *OperationPack) {
+ for _, op := range opp.Operations {
+ _, err := op.Hash()
+ require.NoError(t, err)
+ }
+}
diff --git a/bug/operation_test.go b/bug/operation_test.go
index d5cb5090..f9a7d191 100644
--- a/bug/operation_test.go
+++ b/bug/operation_test.go
@@ -2,6 +2,7 @@ package bug
import (
"testing"
+ "time"
"github.com/MichaelMure/git-bug/identity"
"github.com/MichaelMure/git-bug/repository"
@@ -11,6 +12,9 @@ import (
)
func TestValidate(t *testing.T) {
+ rene := identity.NewIdentity("René Descartes", "rene@descartes.fr")
+ unix := time.Now().Unix()
+
good := []Operation{
NewCreateOp(rene, unix, "title", "message", nil),
NewSetTitleOp(rene, unix, "title2", "title1"),
@@ -65,7 +69,8 @@ func TestValidate(t *testing.T) {
}
func TestMetadata(t *testing.T) {
- op := NewCreateOp(rene, unix, "title", "message", nil)
+ rene := identity.NewIdentity("René Descartes", "rene@descartes.fr")
+ op := NewCreateOp(rene, time.Now().Unix(), "title", "message", nil)
op.SetMetadata("key", "value")
@@ -81,7 +86,9 @@ func TestHash(t *testing.T) {
}
for _, repo := range repos {
- b, op, err := Create(rene, unix, "title", "message")
+ rene := identity.NewBare("René Descartes", "rene@descartes.fr")
+
+ b, op, err := Create(rene, time.Now().Unix(), "title", "message")
require.Nil(t, err)
h1, err := op.Hash()