aboutsummaryrefslogtreecommitdiffstats
path: root/bug/bug.go
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2018-09-15 13:15:00 +0200
committerMichael Muré <batolettre@gmail.com>2018-09-15 13:15:00 +0200
commit7bec0b1f134d213e7505fc2ac03ffea26f2193cc (patch)
treee263cccd84406843eacbc6bd184acdacb25a49d1 /bug/bug.go
parentb478cd1bcb4756b20f7f4b15fcf81f23e1a60a02 (diff)
downloadgit-bug-7bec0b1f134d213e7505fc2ac03ffea26f2193cc.tar.gz
bug: add a data validation process to avoid merging incorrect operations
Diffstat (limited to 'bug/bug.go')
-rw-r--r--bug/bug.go28
1 files changed, 16 insertions, 12 deletions
diff --git a/bug/bug.go b/bug/bug.go
index e548757e..3342ecfa 100644
--- a/bug/bug.go
+++ b/bug/bug.go
@@ -2,13 +2,13 @@ package bug
import (
"encoding/json"
- "errors"
"fmt"
"strings"
"github.com/MichaelMure/git-bug/repository"
"github.com/MichaelMure/git-bug/util/git"
"github.com/MichaelMure/git-bug/util/lamport"
+ "github.com/pkg/errors"
)
const bugsRefPattern = "refs/bugs/"
@@ -279,31 +279,31 @@ func refsToIds(refs []string) []string {
return ids
}
-// IsValid check if the Bug data is valid
-func (bug *Bug) IsValid() bool {
+// Validate check if the Bug data is valid
+func (bug *Bug) Validate() error {
// non-empty
if len(bug.packs) == 0 && bug.staging.IsEmpty() {
- return false
+ return fmt.Errorf("bug has no operations")
}
- // check if each pack is valid
+ // check if each pack and operations are valid
for _, pack := range bug.packs {
- if !pack.IsValid() {
- return false
+ if err := pack.Validate(); err != nil {
+ return err
}
}
// check if staging is valid if needed
if !bug.staging.IsEmpty() {
- if !bug.staging.IsValid() {
- return false
+ if err := bug.staging.Validate(); err != nil {
+ return errors.Wrap(err, "staging")
}
}
// The very first Op should be a CreateOp
firstOp := bug.FirstOp()
if firstOp == nil || firstOp.OpType() != CreateOp {
- return false
+ return fmt.Errorf("first operation should be a Create op")
}
// Check that there is no more CreateOp op
@@ -316,10 +316,10 @@ func (bug *Bug) IsValid() bool {
}
if createCount != 1 {
- return false
+ return fmt.Errorf("only one Create op allowed")
}
- return true
+ return nil
}
// Append an operation into the staging area, to be committed later
@@ -338,6 +338,10 @@ func (bug *Bug) Commit(repo repository.Repo) error {
return fmt.Errorf("can't commit a bug with no pending operation")
}
+ if err := bug.Validate(); err != nil {
+ return errors.Wrap(err, "can't commit a bug with invalid data")
+ }
+
// Write the Ops as a Git blob containing the serialized array
hash, err := bug.staging.Write(repo)
if err != nil {