diff options
Diffstat (limited to 'bug')
-rw-r--r-- | bug/bug.go | 21 | ||||
-rw-r--r-- | bug/comment.go | 4 | ||||
-rw-r--r-- | bug/interface.go | 6 | ||||
-rw-r--r-- | bug/operation.go | 6 | ||||
-rw-r--r-- | bug/operation_pack.go | 6 | ||||
-rw-r--r-- | bug/operations/add_comment.go | 52 | ||||
-rw-r--r-- | bug/operations/create.go | 57 | ||||
-rw-r--r-- | bug/operations/create_test.go | 33 | ||||
-rw-r--r-- | bug/operations/label_change.go | 128 | ||||
-rw-r--r-- | bug/operations/operations.go | 12 | ||||
-rw-r--r-- | bug/operations/set_status.go | 39 | ||||
-rw-r--r-- | bug/operations/set_title.go | 52 |
12 files changed, 22 insertions, 394 deletions
@@ -6,7 +6,8 @@ import ( "strings" "github.com/MichaelMure/git-bug/repository" - "github.com/MichaelMure/git-bug/util" + "github.com/MichaelMure/git-bug/util/git" + "github.com/MichaelMure/git-bug/util/lamport" ) const bugsRefPattern = "refs/bugs/" @@ -34,14 +35,14 @@ type Bug struct { // A Lamport clock is a logical clock that allow to order event // inside a distributed system. // It must be the first field in this struct due to https://github.com/golang/go/issues/599 - createTime util.LamportTime - editTime util.LamportTime + createTime lamport.Time + editTime lamport.Time // Id used as unique identifier id string - lastCommit util.Hash - rootPack util.Hash + lastCommit git.Hash + rootPack git.Hash // all the committed operations packs []OperationPack @@ -173,10 +174,10 @@ func readBug(repo repository.Repo, ref string) (*Bug, error) { if bug.rootPack == "" { bug.rootPack = rootEntry.Hash - bug.createTime = util.LamportTime(createTime) + bug.createTime = lamport.Time(createTime) } - bug.editTime = util.LamportTime(editTime) + bug.editTime = lamport.Time(editTime) // Update the clocks if err := repo.CreateWitness(bug.createTime); err != nil { @@ -446,7 +447,7 @@ func (bug *Bug) Commit(repo repository.Repo) error { func makeMediaTree(pack OperationPack) []repository.TreeEntry { var tree []repository.TreeEntry counter := 0 - added := make(map[util.Hash]interface{}) + added := make(map[git.Hash]interface{}) for _, ops := range pack.Operations { for _, file := range ops.Files() { @@ -576,12 +577,12 @@ func (bug *Bug) HumanId() string { } // CreateLamportTime return the Lamport time of creation -func (bug *Bug) CreateLamportTime() util.LamportTime { +func (bug *Bug) CreateLamportTime() lamport.Time { return bug.createTime } // EditLamportTime return the Lamport time of the last edit -func (bug *Bug) EditLamportTime() util.LamportTime { +func (bug *Bug) EditLamportTime() lamport.Time { return bug.editTime } diff --git a/bug/comment.go b/bug/comment.go index ef5d9ef7..72eed1a0 100644 --- a/bug/comment.go +++ b/bug/comment.go @@ -1,7 +1,7 @@ package bug import ( - "github.com/MichaelMure/git-bug/util" + "github.com/MichaelMure/git-bug/util/git" "github.com/dustin/go-humanize" "time" ) @@ -10,7 +10,7 @@ import ( type Comment struct { Author Person Message string - Files []util.Hash + Files []git.Hash // 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. diff --git a/bug/interface.go b/bug/interface.go index 79333d07..c397bfbc 100644 --- a/bug/interface.go +++ b/bug/interface.go @@ -2,7 +2,7 @@ package bug import ( "github.com/MichaelMure/git-bug/repository" - "github.com/MichaelMure/git-bug/util" + "github.com/MichaelMure/git-bug/util/lamport" ) type Interface interface { @@ -41,10 +41,10 @@ type Interface interface { Compile() Snapshot // CreateLamportTime return the Lamport time of creation - CreateLamportTime() util.LamportTime + CreateLamportTime() lamport.Time // EditLamportTime return the Lamport time of the last edit - EditLamportTime() util.LamportTime + EditLamportTime() lamport.Time } func bugFromInterface(bug Interface) *Bug { diff --git a/bug/operation.go b/bug/operation.go index 00313dc0..f6c63144 100644 --- a/bug/operation.go +++ b/bug/operation.go @@ -1,7 +1,7 @@ package bug import ( - "github.com/MichaelMure/git-bug/util" + "github.com/MichaelMure/git-bug/util/git" "time" ) @@ -28,7 +28,7 @@ type Operation interface { // Apply the operation to a Snapshot to create the final state Apply(snapshot Snapshot) Snapshot // Files return the files needed by this operation - Files() []util.Hash + Files() []git.Hash // TODO: data validation (ex: a title is a single line) // Validate() bool @@ -66,6 +66,6 @@ func (op OpBase) GetUnixTime() int64 { } // Files return the files needed by this operation -func (op OpBase) Files() []util.Hash { +func (op OpBase) Files() []git.Hash { return nil } diff --git a/bug/operation_pack.go b/bug/operation_pack.go index 307c0109..8568fa16 100644 --- a/bug/operation_pack.go +++ b/bug/operation_pack.go @@ -5,7 +5,7 @@ import ( "encoding/gob" "github.com/MichaelMure/git-bug/repository" - "github.com/MichaelMure/git-bug/util" + "github.com/MichaelMure/git-bug/util/git" ) // OperationPack represent an ordered set of operation to apply @@ -18,7 +18,7 @@ type OperationPack struct { Operations []Operation // Private field so not serialized by gob - commitHash util.Hash + commitHash git.Hash } // ParseOperationPack will deserialize an OperationPack from raw bytes @@ -68,7 +68,7 @@ func (opp *OperationPack) IsValid() bool { // Write will serialize and store the OperationPack as a git blob and return // its hash -func (opp *OperationPack) Write(repo repository.Repo) (util.Hash, error) { +func (opp *OperationPack) Write(repo repository.Repo) (git.Hash, error) { data, err := opp.Serialize() if err != nil { diff --git a/bug/operations/add_comment.go b/bug/operations/add_comment.go deleted file mode 100644 index b4126a8e..00000000 --- a/bug/operations/add_comment.go +++ /dev/null @@ -1,52 +0,0 @@ -package operations - -import ( - "github.com/MichaelMure/git-bug/bug" - "github.com/MichaelMure/git-bug/util" -) - -// AddCommentOperation will add a new comment in the bug - -var _ bug.Operation = AddCommentOperation{} - -type AddCommentOperation struct { - bug.OpBase - Message string - // TODO: change for a map[string]util.hash to store the filename ? - files []util.Hash -} - -func (op AddCommentOperation) Apply(snapshot bug.Snapshot) bug.Snapshot { - comment := bug.Comment{ - Message: op.Message, - Author: op.Author, - Files: op.files, - UnixTime: op.UnixTime, - } - - snapshot.Comments = append(snapshot.Comments, comment) - - return snapshot -} - -func (op AddCommentOperation) Files() []util.Hash { - return op.files -} - -func NewAddCommentOp(author bug.Person, message string, files []util.Hash) AddCommentOperation { - return AddCommentOperation{ - OpBase: bug.NewOpBase(bug.AddCommentOp, author), - Message: message, - files: files, - } -} - -// Convenience function to apply the operation -func Comment(b bug.Interface, author bug.Person, message string) { - CommentWithFiles(b, author, message, nil) -} - -func CommentWithFiles(b bug.Interface, author bug.Person, message string, files []util.Hash) { - addCommentOp := NewAddCommentOp(author, message, files) - b.Append(addCommentOp) -} diff --git a/bug/operations/create.go b/bug/operations/create.go deleted file mode 100644 index ecbafb6f..00000000 --- a/bug/operations/create.go +++ /dev/null @@ -1,57 +0,0 @@ -package operations - -import ( - "github.com/MichaelMure/git-bug/bug" - "github.com/MichaelMure/git-bug/util" -) - -// CreateOperation define the initial creation of a bug - -var _ bug.Operation = CreateOperation{} - -type CreateOperation struct { - bug.OpBase - Title string - Message string - files []util.Hash -} - -func (op CreateOperation) Apply(snapshot bug.Snapshot) bug.Snapshot { - snapshot.Title = op.Title - snapshot.Comments = []bug.Comment{ - { - Message: op.Message, - Author: op.Author, - UnixTime: op.UnixTime, - }, - } - snapshot.Author = op.Author - snapshot.CreatedAt = op.Time() - return snapshot -} - -func (op CreateOperation) Files() []util.Hash { - return op.files -} - -func NewCreateOp(author bug.Person, title, message string, files []util.Hash) CreateOperation { - return CreateOperation{ - OpBase: bug.NewOpBase(bug.CreateOp, author), - Title: title, - Message: message, - files: files, - } -} - -// Convenience function to apply the operation -func Create(author bug.Person, title, message string) (*bug.Bug, error) { - return CreateWithFiles(author, title, message, nil) -} - -func CreateWithFiles(author bug.Person, title, message string, files []util.Hash) (*bug.Bug, error) { - newBug := bug.NewBug() - createOp := NewCreateOp(author, title, message, files) - newBug.Append(createOp) - - return newBug, nil -} diff --git a/bug/operations/create_test.go b/bug/operations/create_test.go deleted file mode 100644 index a20472d3..00000000 --- a/bug/operations/create_test.go +++ /dev/null @@ -1,33 +0,0 @@ -package operations - -import ( - "github.com/MichaelMure/git-bug/bug" - "reflect" - "testing" -) - -func TestCreate(t *testing.T) { - snapshot := bug.Snapshot{} - - var rene = bug.Person{ - Name: "René Descartes", - Email: "rene@descartes.fr", - } - - create := NewCreateOp(rene, "title", "message", nil) - - snapshot = create.Apply(snapshot) - - expected := bug.Snapshot{ - Title: "title", - Comments: []bug.Comment{ - {Author: rene, Message: "message", UnixTime: create.UnixTime}, - }, - Author: rene, - CreatedAt: create.Time(), - } - - if !reflect.DeepEqual(snapshot, expected) { - t.Fatalf("%v different than %v", snapshot, expected) - } -} diff --git a/bug/operations/label_change.go b/bug/operations/label_change.go deleted file mode 100644 index 551b8be0..00000000 --- a/bug/operations/label_change.go +++ /dev/null @@ -1,128 +0,0 @@ -package operations - -import ( - "fmt" - "io" - "io/ioutil" - "sort" - - "github.com/MichaelMure/git-bug/bug" -) - -var _ bug.Operation = LabelChangeOperation{} - -// LabelChangeOperation define a Bug operation to add or remove labels -type LabelChangeOperation struct { - bug.OpBase - Added []bug.Label - Removed []bug.Label -} - -// Apply apply the operation -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 -} - -func NewLabelChangeOperation(author bug.Person, added, removed []bug.Label) LabelChangeOperation { - return LabelChangeOperation{ - OpBase: bug.NewOpBase(bug.LabelChangeOp, author), - Added: added, - Removed: removed, - } -} - -// ChangeLabels is a convenience function to apply the operation -func ChangeLabels(out io.Writer, b bug.Interface, author bug.Person, add, remove []string) error { - // TODO: return a channel of result (like MergeAll) instead of formatting the result for the upper layers - var added, removed []bug.Label - - if out == nil { - out = ioutil.Discard - } - - snap := b.Compile() - - for _, str := range add { - label := bug.Label(str) - - // check for duplicate - if labelExist(added, label) { - fmt.Fprintf(out, "label \"%s\" is a duplicate\n", str) - continue - } - - // check that the label doesn't already exist - if labelExist(snap.Labels, label) { - fmt.Fprintf(out, "label \"%s\" is already set on this bug\n", str) - continue - } - - added = append(added, label) - } - - for _, str := range remove { - label := bug.Label(str) - - // check for duplicate - if labelExist(removed, label) { - fmt.Fprintf(out, "label \"%s\" is a duplicate\n", str) - continue - } - - // check that the label actually exist - if !labelExist(snap.Labels, label) { - fmt.Fprintf(out, "label \"%s\" doesn't exist on this bug\n", str) - continue - } - - removed = append(removed, label) - } - - if len(added) == 0 && len(removed) == 0 { - return fmt.Errorf("no label added or removed") - } - - labelOp := NewLabelChangeOperation(author, added, removed) - - b.Append(labelOp) - - return nil -} - -func labelExist(labels []bug.Label, label bug.Label) bool { - for _, l := range labels { - if l == label { - return true - } - } - - return false -} diff --git a/bug/operations/operations.go b/bug/operations/operations.go deleted file mode 100644 index 0bfd3b84..00000000 --- a/bug/operations/operations.go +++ /dev/null @@ -1,12 +0,0 @@ -package operations - -import "encoding/gob" - -// Package initialisation used to register operation's type for (de)serialization -func init() { - gob.Register(AddCommentOperation{}) - gob.Register(CreateOperation{}) - gob.Register(SetTitleOperation{}) - gob.Register(SetStatusOperation{}) - gob.Register(LabelChangeOperation{}) -} diff --git a/bug/operations/set_status.go b/bug/operations/set_status.go deleted file mode 100644 index bafcf5ee..00000000 --- a/bug/operations/set_status.go +++ /dev/null @@ -1,39 +0,0 @@ -package operations - -import ( - "github.com/MichaelMure/git-bug/bug" -) - -// SetStatusOperation will change the status of a bug - -var _ bug.Operation = SetStatusOperation{} - -type SetStatusOperation struct { - bug.OpBase - Status bug.Status -} - -func (op SetStatusOperation) Apply(snapshot bug.Snapshot) bug.Snapshot { - snapshot.Status = op.Status - - return snapshot -} - -func NewSetStatusOp(author bug.Person, status bug.Status) SetStatusOperation { - return SetStatusOperation{ - OpBase: bug.NewOpBase(bug.SetStatusOp, author), - Status: status, - } -} - -// Convenience function to apply the operation -func Open(b bug.Interface, author bug.Person) { - op := NewSetStatusOp(author, bug.OpenStatus) - b.Append(op) -} - -// Convenience function to apply the operation -func Close(b bug.Interface, author bug.Person) { - op := NewSetStatusOp(author, bug.ClosedStatus) - b.Append(op) -} diff --git a/bug/operations/set_title.go b/bug/operations/set_title.go deleted file mode 100644 index 5bd6260a..00000000 --- a/bug/operations/set_title.go +++ /dev/null @@ -1,52 +0,0 @@ -package operations - -import ( - "github.com/MichaelMure/git-bug/bug" -) - -// SetTitleOperation will change the title of a bug - -var _ bug.Operation = SetTitleOperation{} - -type SetTitleOperation struct { - bug.OpBase - Title string - Was string -} - -func (op SetTitleOperation) Apply(snapshot bug.Snapshot) bug.Snapshot { - snapshot.Title = op.Title - - return snapshot -} - -func NewSetTitleOp(author bug.Person, title string, was string) SetTitleOperation { - return SetTitleOperation{ - OpBase: bug.NewOpBase(bug.SetTitleOp, author), - Title: title, - Was: was, - } -} - -// Convenience function to apply the operation -func SetTitle(b bug.Interface, author bug.Person, title string) { - it := bug.NewOperationIterator(b) - - var lastTitleOp bug.Operation - for it.Next() { - op := it.Value() - if op.OpType() == bug.SetTitleOp { - lastTitleOp = op - } - } - - var was string - if lastTitleOp != nil { - was = lastTitleOp.(SetTitleOperation).Title - } else { - was = b.FirstOp().(CreateOperation).Title - } - - setTitleOp := NewSetTitleOp(author, title, was) - b.Append(setTitleOp) -} |