snapshot.go

 1package bug
 2
 3import (
 4	"fmt"
 5	"time"
 6
 7	"github.com/MichaelMure/git-bug/util/git"
 8)
 9
10// Snapshot is a compiled form of the Bug data structure used for storage and merge
11type Snapshot struct {
12	id string
13
14	Status    Status
15	Title     string
16	Comments  []Comment
17	Labels    []Label
18	Author    Person
19	CreatedAt time.Time
20
21	Timeline []TimelineItem
22
23	Operations []Operation
24}
25
26// Return the Bug identifier
27func (snap *Snapshot) Id() string {
28	return snap.id
29}
30
31// Return the Bug identifier truncated for human consumption
32func (snap *Snapshot) HumanId() string {
33	return fmt.Sprintf("%.8s", snap.id)
34}
35
36func (snap *Snapshot) Summary() string {
37	return fmt.Sprintf("C:%d L:%d",
38		len(snap.Comments)-1,
39		len(snap.Labels),
40	)
41}
42
43// Return the last time a bug was modified
44func (snap *Snapshot) LastEditTime() time.Time {
45	if len(snap.Operations) == 0 {
46		return time.Unix(0, 0)
47	}
48
49	return snap.Operations[len(snap.Operations)-1].Time()
50}
51
52// Return the last timestamp a bug was modified
53func (snap *Snapshot) LastEditUnix() int64 {
54	if len(snap.Operations) == 0 {
55		return 0
56	}
57
58	return snap.Operations[len(snap.Operations)-1].GetUnixTime()
59}
60
61// SearchTimelineItem will search in the timeline for an item matching the given hash
62func (snap *Snapshot) SearchTimelineItem(hash git.Hash) (TimelineItem, error) {
63	for i := range snap.Timeline {
64		h, err := snap.Timeline[i].Hash()
65		if err != nil {
66			return nil, err
67		}
68
69		if h == hash {
70			return snap.Timeline[i], nil
71		}
72	}
73
74	return nil, fmt.Errorf("timeline item not found")
75}