diff options
author | Michael Muré <batolettre@gmail.com> | 2018-07-18 16:41:09 +0200 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2018-07-18 16:41:09 +0200 |
commit | cfa11372cbcbb5f4553f3dcd8897d5f5207c6507 (patch) | |
tree | a5136685378a9c3310c86700bbeaf34338281f9a /bug/operations | |
parent | ba3281dc9918fa49f10c2a166b5b631a931d2d51 (diff) | |
download | git-bug-cfa11372cbcbb5f4553f3dcd8897d5f5207c6507.tar.gz |
implement label op+command
Diffstat (limited to 'bug/operations')
-rw-r--r-- | bug/operations/label_change.go | 56 | ||||
-rw-r--r-- | bug/operations/operations.go | 1 |
2 files changed, 57 insertions, 0 deletions
diff --git a/bug/operations/label_change.go b/bug/operations/label_change.go new file mode 100644 index 00000000..ca07f6f5 --- /dev/null +++ b/bug/operations/label_change.go @@ -0,0 +1,56 @@ +package operations + +import ( + "github.com/MichaelMure/git-bug/bug" + "sort" +) + +// LabelChangeOperation will add or remove a set of labels + +var _ bug.Operation = LabelChangeOperation{} + +type LabelChangeOperation struct { + bug.OpBase + Added []bug.Label + Removed []bug.Label +} + +func NewLabelChangeOperation(author bug.Person, added, removed []bug.Label) LabelChangeOperation { + return LabelChangeOperation{ + OpBase: bug.NewOpBase(bug.LabelChangeOp, author), + Added: added, + Removed: removed, + } +} + +func (op LabelChangeOperation) Apply(snapshot bug.Snapshot) bug.Snapshot { + // Add in the set +AddLoop: + for _, added := range op.Added { + for _, label := range snapshot.Labels { + if label == added { + // Already exist + continue AddLoop + } + } + + snapshot.Labels = append(snapshot.Labels, added) + } + + // Remove in the set + for _, removed := range op.Removed { + for i, label := range snapshot.Labels { + if label == removed { + snapshot.Labels[i] = snapshot.Labels[len(snapshot.Labels)-1] + snapshot.Labels = snapshot.Labels[:len(snapshot.Labels)-1] + } + } + } + + // Sort + sort.Slice(snapshot.Labels, func(i, j int) bool { + return string(snapshot.Labels[i]) < string(snapshot.Labels[j]) + }) + + return snapshot +} diff --git a/bug/operations/operations.go b/bug/operations/operations.go index 50692952..0bfd3b84 100644 --- a/bug/operations/operations.go +++ b/bug/operations/operations.go @@ -8,4 +8,5 @@ func init() { gob.Register(CreateOperation{}) gob.Register(SetTitleOperation{}) gob.Register(SetStatusOperation{}) + gob.Register(LabelChangeOperation{}) } |