aboutsummaryrefslogtreecommitdiffstats
path: root/bug
diff options
context:
space:
mode:
Diffstat (limited to 'bug')
-rw-r--r--bug/comment.go9
-rw-r--r--bug/op_add_comment.go2
-rw-r--r--bug/op_create.go2
-rw-r--r--bug/op_create_test.go2
-rw-r--r--bug/op_edit_comment.go14
-rw-r--r--bug/time.go9
-rw-r--r--bug/timeline.go81
7 files changed, 76 insertions, 43 deletions
diff --git a/bug/comment.go b/bug/comment.go
index c7168275..db5cc45e 100644
--- a/bug/comment.go
+++ b/bug/comment.go
@@ -3,7 +3,6 @@ package bug
import (
"github.com/MichaelMure/git-bug/util/git"
"github.com/dustin/go-humanize"
- "time"
)
// Comment represent a comment in a Bug
@@ -14,16 +13,14 @@ type Comment struct {
// Creation time of the comment.
// Should be used only for human display, never for ordering as we can't rely on it in a distributed system.
- UnixTime int64
+ UnixTime Timestamp
}
// FormatTimeRel format the UnixTime of the comment for human consumption
func (c Comment) FormatTimeRel() string {
- t := time.Unix(c.UnixTime, 0)
- return humanize.Time(t)
+ return humanize.Time(c.UnixTime.Time())
}
func (c Comment) FormatTime() string {
- t := time.Unix(c.UnixTime, 0)
- return t.Format("Mon Jan 2 15:04:05 2006 +0200")
+ return c.UnixTime.Time().Format("Mon Jan 2 15:04:05 2006 +0200")
}
diff --git a/bug/op_add_comment.go b/bug/op_add_comment.go
index 4594ba70..7f8b8b5b 100644
--- a/bug/op_add_comment.go
+++ b/bug/op_add_comment.go
@@ -30,7 +30,7 @@ func (op *AddCommentOperation) Apply(snapshot *Snapshot) {
Message: op.Message,
Author: op.Author,
Files: op.Files,
- UnixTime: op.UnixTime,
+ UnixTime: Timestamp(op.UnixTime),
}
snapshot.Comments = append(snapshot.Comments, comment)
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}
diff --git a/bug/op_create_test.go b/bug/op_create_test.go
index e9a36cf8..f27f6ee0 100644
--- a/bug/op_create_test.go
+++ b/bug/op_create_test.go
@@ -26,7 +26,7 @@ func TestCreate(t *testing.T) {
t.Fatal(err)
}
- comment := Comment{Author: rene, Message: "message", UnixTime: create.UnixTime}
+ comment := Comment{Author: rene, Message: "message", UnixTime: Timestamp(create.UnixTime)}
expected := Snapshot{
Title: "title",
diff --git a/bug/op_edit_comment.go b/bug/op_edit_comment.go
index c6cfab2b..cb4a2216 100644
--- a/bug/op_edit_comment.go
+++ b/bug/op_edit_comment.go
@@ -57,18 +57,20 @@ func (op *EditCommentOperation) Apply(snapshot *Snapshot) {
return
}
+ comment := Comment{
+ Message: op.Message,
+ Files: op.Files,
+ UnixTime: Timestamp(op.UnixTime),
+ }
+
switch target.(type) {
case *CreateTimelineItem:
item := target.(*CreateTimelineItem)
- newComment := item.LastState()
- newComment.Message = op.Message
- item.History = append(item.History, newComment)
+ item.Append(comment)
case *CommentTimelineItem:
item := target.(*CommentTimelineItem)
- newComment := item.LastState()
- newComment.Message = op.Message
- item.History = append(item.History, newComment)
+ item.Append(comment)
}
snapshot.Comments[commentIndex].Message = op.Message
diff --git a/bug/time.go b/bug/time.go
new file mode 100644
index 00000000..a085e8e9
--- /dev/null
+++ b/bug/time.go
@@ -0,0 +1,9 @@
+package bug
+
+import "time"
+
+type Timestamp int64
+
+func (t Timestamp) Time() time.Time {
+ return time.Unix(int64(t), 0)
+}
diff --git a/bug/timeline.go b/bug/timeline.go
index 0f79958b..d734e18b 100644
--- a/bug/timeline.go
+++ b/bug/timeline.go
@@ -1,50 +1,67 @@
package bug
-import "github.com/MichaelMure/git-bug/util/git"
+import (
+ "github.com/MichaelMure/git-bug/util/git"
+)
type TimelineItem interface {
// Hash return the hash of the item
Hash() (git.Hash, error)
}
+type CommentHistoryStep struct {
+ Message string
+ UnixTime Timestamp
+}
+
// CreateTimelineItem replace a Create operation in the Timeline and hold its edition history
type CreateTimelineItem struct {
- hash git.Hash
- History []Comment
+ CommentTimelineItem
}
func NewCreateTimelineItem(hash git.Hash, comment Comment) *CreateTimelineItem {
return &CreateTimelineItem{
- hash: hash,
- History: []Comment{
- comment,
+ CommentTimelineItem: CommentTimelineItem{
+ hash: hash,
+ Author: comment.Author,
+ Message: comment.Message,
+ Files: comment.Files,
+ CreatedAt: comment.UnixTime,
+ LastEdit: comment.UnixTime,
+ History: []CommentHistoryStep{
+ {
+ Message: comment.Message,
+ UnixTime: comment.UnixTime,
+ },
+ },
},
}
}
-func (c *CreateTimelineItem) Hash() (git.Hash, error) {
- return c.hash, nil
-}
-
-func (c *CreateTimelineItem) LastState() Comment {
- if len(c.History) == 0 {
- panic("no history yet")
- }
-
- return c.History[len(c.History)-1]
-}
-
// CommentTimelineItem replace a Comment in the Timeline and hold its edition history
type CommentTimelineItem struct {
- hash git.Hash
- History []Comment
+ hash git.Hash
+ Author Person
+ Message string
+ Files []git.Hash
+ CreatedAt Timestamp
+ LastEdit Timestamp
+ History []CommentHistoryStep
}
func NewCommentTimelineItem(hash git.Hash, comment Comment) *CommentTimelineItem {
return &CommentTimelineItem{
- hash: hash,
- History: []Comment{
- comment,
+ hash: hash,
+ Author: comment.Author,
+ Message: comment.Message,
+ Files: comment.Files,
+ CreatedAt: comment.UnixTime,
+ LastEdit: comment.UnixTime,
+ History: []CommentHistoryStep{
+ {
+ Message: comment.Message,
+ UnixTime: comment.UnixTime,
+ },
},
}
}
@@ -53,10 +70,18 @@ func (c *CommentTimelineItem) Hash() (git.Hash, error) {
return c.hash, nil
}
-func (c *CommentTimelineItem) LastState() Comment {
- if len(c.History) == 0 {
- panic("no history yet")
- }
+// Append will append a new comment in the history and update the other values
+func (c *CommentTimelineItem) Append(comment Comment) {
+ c.Message = comment.Message
+ c.Files = comment.Files
+ c.LastEdit = comment.UnixTime
+ c.History = append(c.History, CommentHistoryStep{
+ Message: comment.Message,
+ UnixTime: comment.UnixTime,
+ })
+}
- return c.History[len(c.History)-1]
+// Edited say if the comment was edited
+func (c *CommentTimelineItem) Edited() bool {
+ return len(c.History) > 1
}