diff --git a/bug/comment.go b/bug/comment.go index f7a6eada769684cf479854435fc7ebe7c99f0a81..5db0b18d8a11df3fe2170650149f52dea3807b0c 100644 --- a/bug/comment.go +++ b/bug/comment.go @@ -9,6 +9,7 @@ import ( // Comment represent a comment in a Bug type Comment struct { + id string Author identity.Interface Message string Files []git.Hash @@ -18,6 +19,21 @@ type Comment struct { UnixTime timestamp.Timestamp } +// Id return the Comment identifier +func (c Comment) Id() string { + if c.id == "" { + // simply panic as it would be a coding error + // (using an id of an identity not stored yet) + panic("no id yet") + } + return c.id +} + +// HumanId return the Comment identifier truncated for human consumption +func (c Comment) HumanId() string { + return FormatHumanID(c.Id()) +} + // FormatTimeRel format the UnixTime of the comment for human consumption func (c Comment) FormatTimeRel() string { return humanize.Time(c.UnixTime.Time()) diff --git a/bug/op_add_comment.go b/bug/op_add_comment.go index 9ecef941efbb0d8bd47b568c1e2fdfdd5df37d8d..c1e0838b2c6984febf238a90e45778f6c8b0c7a9 100644 --- a/bug/op_add_comment.go +++ b/bug/op_add_comment.go @@ -29,7 +29,15 @@ func (op *AddCommentOperation) Hash() (git.Hash, error) { } func (op *AddCommentOperation) Apply(snapshot *Snapshot) { + hash, err := op.Hash() + if err != nil { + // Should never error unless a programming error happened + // (covered in OpBase.Validate()) + panic(err) + } + comment := Comment{ + id: string(hash), Message: op.Message, Author: op.Author, Files: op.Files, @@ -38,13 +46,6 @@ func (op *AddCommentOperation) Apply(snapshot *Snapshot) { snapshot.Comments = append(snapshot.Comments, comment) - hash, err := op.Hash() - if err != nil { - // Should never error unless a programming error happened - // (covered in OpBase.Validate()) - panic(err) - } - item := &AddCommentTimelineItem{ CommentTimelineItem: NewCommentTimelineItem(hash, comment), } diff --git a/bug/op_create.go b/bug/op_create.go index 42d40f71ea76e253022392971091c6953ed65538..d5852db9a8eb68cd97bab3dc10a9d8ed8a2e61c5 100644 --- a/bug/op_create.go +++ b/bug/op_create.go @@ -30,9 +30,17 @@ func (op *CreateOperation) Hash() (git.Hash, error) { } func (op *CreateOperation) Apply(snapshot *Snapshot) { + hash, err := op.Hash() + if err != nil { + // Should never error unless a programming error happened + // (covered in OpBase.Validate()) + panic(err) + } + snapshot.Title = op.Title comment := Comment{ + id: string(hash), Message: op.Message, Author: op.Author, UnixTime: timestamp.Timestamp(op.UnixTime), @@ -42,13 +50,6 @@ func (op *CreateOperation) Apply(snapshot *Snapshot) { 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{ &CreateTimelineItem{ CommentTimelineItem: NewCommentTimelineItem(hash, comment), diff --git a/bug/op_create_test.go b/bug/op_create_test.go index 92ac191dc265d62e96bf972eafeec3dedd5955d2..aa1d8c104b3d60b4c96e24364a31c46a0b3ebf71 100644 --- a/bug/op_create_test.go +++ b/bug/op_create_test.go @@ -23,7 +23,12 @@ func TestCreate(t *testing.T) { hash, err := create.Hash() assert.NoError(t, err) - comment := Comment{Author: rene, Message: "message", UnixTime: timestamp.Timestamp(create.UnixTime)} + comment := Comment{ + id: string(hash), + Author: rene, + Message: "message", + UnixTime: timestamp.Timestamp(create.UnixTime), + } expected := Snapshot{ Title: "title", diff --git a/bug/op_edit_comment.go b/bug/op_edit_comment.go index 527b7440dd1e06e2e514c25dc85ed47787d3962f..5a223e01a68e433cfc10070383257886d6fefce1 100644 --- a/bug/op_edit_comment.go +++ b/bug/op_edit_comment.go @@ -57,6 +57,7 @@ func (op *EditCommentOperation) Apply(snapshot *Snapshot) { } comment := Comment{ + id: string(op.Target), Message: op.Message, Files: op.Files, UnixTime: timestamp.Timestamp(op.UnixTime), diff --git a/bug/operation.go b/bug/operation.go index cc5b0007504d9ba5eb01761502f00b9f28e38598..8e77eed83c97d71584eaad8596343727045acd44 100644 --- a/bug/operation.go +++ b/bug/operation.go @@ -60,6 +60,9 @@ func hashRaw(data []byte) git.Hash { // hash compute the hash of the serialized operation func hashOperation(op Operation) (git.Hash, error) { + // TODO: this might not be the best idea: if a single bit change in the output of json.Marshal, this will break. + // Idea: hash the segment of serialized data (= immutable) instead of the go object in memory + base := op.base() if base.hash != "" { diff --git a/commands/comment.go b/commands/comment.go index 08751e275ec25d7e1ae5099b79e5d09bba24900f..cb9f36916da899dafe57f901efa945f2cf261c06 100644 --- a/commands/comment.go +++ b/commands/comment.go @@ -39,6 +39,7 @@ func commentsTextOutput(comments []bug.Comment) { } fmt.Printf("Author: %s\n", colors.Magenta(comment.Author.DisplayName())) + fmt.Printf("Id: %s\n", colors.Cyan(comment.HumanId())) fmt.Printf("Date: %s\n\n", comment.FormatTime()) fmt.Println(text.LeftPad(comment.Message, 4)) } diff --git a/identity/identity_stub.go b/identity/identity_stub.go index 592eab3080d3bd37016a09076c8d161dda4bf649..a2cee0ad6c818c2421f6b06981282cc2d7c00f92 100644 --- a/identity/identity_stub.go +++ b/identity/identity_stub.go @@ -20,6 +20,7 @@ type IdentityStub struct { } func (i *IdentityStub) MarshalJSON() ([]byte, error) { + // TODO: add a type marker return json.Marshal(struct { Id string `json:"id"` }{