aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2018-07-15 09:25:29 +0200
committerMichael Muré <batolettre@gmail.com>2018-07-15 09:25:29 +0200
commitcbff4b8aba9bc5207d07523181f2be8e94ece881 (patch)
treee4ee1b623cb4d62c186a678853a6ef93fbb286bb
parent7c5c567ed5963b2ad2bfa6edcdbadf4fbbc6d4db (diff)
downloadgit-bug-cbff4b8aba9bc5207d07523181f2be8e94ece881.tar.gz
add a way to load a bug from a prefix
-rw-r--r--bug/bug.go31
1 files changed, 31 insertions, 0 deletions
diff --git a/bug/bug.go b/bug/bug.go
index d0afa830..f536716e 100644
--- a/bug/bug.go
+++ b/bug/bug.go
@@ -7,6 +7,7 @@ import (
"github.com/MichaelMure/git-bug/repository"
"github.com/MichaelMure/git-bug/util"
"github.com/kevinburke/go.uuid"
+ "strings"
)
const BugsRefPattern = "refs/bugs/"
@@ -33,6 +34,8 @@ type Bug struct {
// Create a new Bug
func NewBug() (*Bug, error) {
+ // TODO: replace with simple random bytes + hash
+
// Creating UUID Version 4
unique, err := uuid.ID4()
@@ -51,6 +54,34 @@ func NewBug() (*Bug, error) {
}, nil
}
+// Find an existing Bug matching a prefix
+func FindBug(repo repository.Repo, prefix string) (*Bug, error) {
+ refs, err := repo.ListRefs(BugsRefPattern)
+
+ if err != nil {
+ return nil, err
+ }
+
+ // preallocate but empty
+ matching := make([]string, 0, 5)
+
+ for _, ref := range refs {
+ if strings.HasPrefix(ref, prefix) {
+ matching = append(matching, ref)
+ }
+ }
+
+ if len(matching) == 0 {
+ return nil, errors.New("No matching bug found.")
+ }
+
+ if len(matching) > 1 {
+ return nil, fmt.Errorf("Multiple matching bug found:\n%s", strings.Join(matching, "\n"))
+ }
+
+ return ReadBug(repo, matching[0])
+}
+
// Read and parse a Bug from git
func ReadBug(repo repository.Repo, id string) (*Bug, error) {
hashes, err := repo.ListCommits(BugsRefPattern + id)