From cbff4b8aba9bc5207d07523181f2be8e94ece881 Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Sun, 15 Jul 2018 09:25:29 +0200 Subject: add a way to load a bug from a prefix --- bug/bug.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'bug') 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) -- cgit