From 8ec1dd092656aed5dae22a0301bd3f85b5dabb88 Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Mon, 1 Oct 2018 23:34:45 +0200 Subject: github: working incremental + comment history for the first comment --- cache/bug_cache.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) (limited to 'cache/bug_cache.go') diff --git a/cache/bug_cache.go b/cache/bug_cache.go index b7c80a0c..52e9eafb 100644 --- a/cache/bug_cache.go +++ b/cache/bug_cache.go @@ -1,6 +1,8 @@ package cache import ( + "fmt" + "strings" "time" "github.com/MichaelMure/git-bug/bug" @@ -35,6 +37,51 @@ func (c *BugCache) notifyUpdated() error { return c.repoCache.bugUpdated(c.bug.Id()) } +var ErrNoMatchingOp = fmt.Errorf("no matching operation found") + +type ErrMultipleMatchOp struct { + Matching []git.Hash +} + +func (e ErrMultipleMatchOp) Error() string { + casted := make([]string, len(e.Matching)) + + for i := range e.Matching { + casted[i] = string(e.Matching[i]) + } + + return fmt.Sprintf("Multiple matching operation found:\n%s", strings.Join(casted, "\n")) +} + +// ResolveTargetWithMetadata will find an operation that has the matching metadata +func (c *BugCache) ResolveTargetWithMetadata(key string, value string) (git.Hash, error) { + // preallocate but empty + matching := make([]git.Hash, 0, 5) + + it := bug.NewOperationIterator(c.bug) + for it.Next() { + op := it.Value() + opValue, ok := op.GetMetadata(key) + if ok && value == opValue { + h, err := op.Hash() + if err != nil { + return "", err + } + matching = append(matching, h) + } + } + + if len(matching) == 0 { + return "", ErrNoMatchingOp + } + + if len(matching) > 1 { + return "", ErrMultipleMatchOp{Matching: matching} + } + + return matching[0], nil +} + func (c *BugCache) AddComment(message string) error { return c.AddCommentWithFiles(message, nil) } -- cgit