From 1bf268cebc84a9de1e538cbb54bcc0f434022192 Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Fri, 28 Sep 2018 20:39:39 +0200 Subject: merge package operations into bug, they are tightly coupled anyway --- bug/op_create.go | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 bug/op_create.go (limited to 'bug/op_create.go') diff --git a/bug/op_create.go b/bug/op_create.go new file mode 100644 index 00000000..2852a519 --- /dev/null +++ b/bug/op_create.go @@ -0,0 +1,93 @@ +package bug + +import ( + "fmt" + "strings" + + "github.com/MichaelMure/git-bug/util/git" + "github.com/MichaelMure/git-bug/util/text" +) + +// CreateOperation define the initial creation of a bug + +var _ Operation = CreateOperation{} + +type CreateOperation struct { + *OpBase + Title string `json:"title"` + Message string `json:"message"` + Files []git.Hash `json:"files"` +} + +func (op CreateOperation) base() *OpBase { + return op.OpBase +} + +func (op CreateOperation) Apply(snapshot Snapshot) Snapshot { + snapshot.Title = op.Title + snapshot.Comments = []Comment{ + { + Message: op.Message, + Author: op.Author, + UnixTime: op.UnixTime, + }, + } + snapshot.Author = op.Author + snapshot.CreatedAt = op.Time() + return snapshot +} + +func (op CreateOperation) GetFiles() []git.Hash { + return op.Files +} + +func (op CreateOperation) Validate() error { + if err := opBaseValidate(op, CreateOp); err != nil { + return err + } + + if text.Empty(op.Title) { + return fmt.Errorf("title is empty") + } + + if strings.Contains(op.Title, "\n") { + return fmt.Errorf("title should be a single line") + } + + if !text.Safe(op.Title) { + return fmt.Errorf("title is not fully printable") + } + + if !text.Safe(op.Message) { + return fmt.Errorf("message is not fully printable") + } + + return nil +} + +func NewCreateOp(author Person, unixTime int64, title, message string, files []git.Hash) CreateOperation { + return CreateOperation{ + OpBase: newOpBase(CreateOp, author, unixTime), + Title: title, + Message: message, + Files: files, + } +} + +// Convenience function to apply the operation +func Create(author Person, unixTime int64, title, message string) (*Bug, error) { + return CreateWithFiles(author, unixTime, title, message, nil) +} + +func CreateWithFiles(author Person, unixTime int64, title, message string, files []git.Hash) (*Bug, error) { + newBug := NewBug() + createOp := NewCreateOp(author, unixTime, title, message, files) + + if err := createOp.Validate(); err != nil { + return nil, err + } + + newBug.Append(createOp) + + return newBug, nil +} -- cgit From 794d014fae9a78bd8664e6628a20902bd6dc767a Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Fri, 28 Sep 2018 23:51:47 +0200 Subject: bug: define a hash-based identifier for an operation --- bug/op_create.go | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'bug/op_create.go') diff --git a/bug/op_create.go b/bug/op_create.go index 2852a519..c65b3bd8 100644 --- a/bug/op_create.go +++ b/bug/op_create.go @@ -23,6 +23,10 @@ func (op CreateOperation) base() *OpBase { return op.OpBase } +func (op CreateOperation) Hash() (git.Hash, error) { + return hashOperation(op) +} + func (op CreateOperation) Apply(snapshot Snapshot) Snapshot { snapshot.Title = op.Title snapshot.Comments = []Comment{ -- cgit From 41e61a67b63e4d6c517005cf6f427115a664bdb5 Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Sat, 29 Sep 2018 11:28:18 +0200 Subject: bug: apply an operation with a pointer to the snapshot --- bug/op_create.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'bug/op_create.go') diff --git a/bug/op_create.go b/bug/op_create.go index c65b3bd8..70ca7242 100644 --- a/bug/op_create.go +++ b/bug/op_create.go @@ -27,7 +27,7 @@ func (op CreateOperation) Hash() (git.Hash, error) { return hashOperation(op) } -func (op CreateOperation) Apply(snapshot Snapshot) Snapshot { +func (op CreateOperation) Apply(snapshot *Snapshot) { snapshot.Title = op.Title snapshot.Comments = []Comment{ { @@ -38,7 +38,6 @@ func (op CreateOperation) Apply(snapshot Snapshot) Snapshot { } snapshot.Author = op.Author snapshot.CreatedAt = op.Time() - return snapshot } func (op CreateOperation) GetFiles() []git.Hash { -- cgit From c46d01f8c10e6363b680fa6876e91bd8eaf3bb3e Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Sat, 29 Sep 2018 20:41:19 +0200 Subject: bug: implement comment edition - add a new operation - add a new "timeline" in the snapshot that hold a processed version of the operations --- bug/op_create.go | 43 +++++++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 16 deletions(-) (limited to 'bug/op_create.go') diff --git a/bug/op_create.go b/bug/op_create.go index 70ca7242..5c41eb7c 100644 --- a/bug/op_create.go +++ b/bug/op_create.go @@ -8,10 +8,9 @@ import ( "github.com/MichaelMure/git-bug/util/text" ) -// CreateOperation define the initial creation of a bug - -var _ Operation = CreateOperation{} +var _ Operation = &CreateOperation{} +// CreateOperation define the initial creation of a bug type CreateOperation struct { *OpBase Title string `json:"title"` @@ -19,32 +18,44 @@ type CreateOperation struct { Files []git.Hash `json:"files"` } -func (op CreateOperation) base() *OpBase { +func (op *CreateOperation) base() *OpBase { return op.OpBase } -func (op CreateOperation) Hash() (git.Hash, error) { +func (op *CreateOperation) Hash() (git.Hash, error) { return hashOperation(op) } -func (op CreateOperation) Apply(snapshot *Snapshot) { +func (op *CreateOperation) Apply(snapshot *Snapshot) { snapshot.Title = op.Title - snapshot.Comments = []Comment{ - { - Message: op.Message, - Author: op.Author, - UnixTime: op.UnixTime, - }, + + comment := Comment{ + Message: op.Message, + Author: op.Author, + UnixTime: op.UnixTime, } + + snapshot.Comments = []Comment{comment} snapshot.Author = op.Author snapshot.CreatedAt = op.Time() + + hash, err := op.Hash() + if err != nil { + // Should never error unless a programming error happened + // (covered in OpBase.Validate()) + panic(err) + } + + snapshot.Timeline = []TimelineItem{ + NewCreateTimelineItem(hash, comment), + } } -func (op CreateOperation) GetFiles() []git.Hash { +func (op *CreateOperation) GetFiles() []git.Hash { return op.Files } -func (op CreateOperation) Validate() error { +func (op *CreateOperation) Validate() error { if err := opBaseValidate(op, CreateOp); err != nil { return err } @@ -68,8 +79,8 @@ func (op CreateOperation) Validate() error { return nil } -func NewCreateOp(author Person, unixTime int64, title, message string, files []git.Hash) CreateOperation { - return CreateOperation{ +func NewCreateOp(author Person, unixTime int64, title, message string, files []git.Hash) *CreateOperation { + return &CreateOperation{ OpBase: newOpBase(CreateOp, author, unixTime), Title: title, Message: message, -- cgit From 037f5bf50b2bb2b020620413d186b6acf47a0b61 Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Sun, 30 Sep 2018 11:00:39 +0200 Subject: timeline: various minor improvements --- bug/op_create.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bug/op_create.go') diff --git a/bug/op_create.go b/bug/op_create.go index 5c41eb7c..0553137f 100644 --- a/bug/op_create.go +++ b/bug/op_create.go @@ -32,7 +32,7 @@ func (op *CreateOperation) Apply(snapshot *Snapshot) { comment := Comment{ Message: op.Message, Author: op.Author, - UnixTime: op.UnixTime, + UnixTime: Timestamp(op.UnixTime), } snapshot.Comments = []Comment{comment} -- cgit