diff options
author | Michael Muré <batolettre@gmail.com> | 2018-07-15 09:25:29 +0200 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2018-07-15 09:25:29 +0200 |
commit | cbff4b8aba9bc5207d07523181f2be8e94ece881 (patch) | |
tree | e4ee1b623cb4d62c186a678853a6ef93fbb286bb /bug/bug.go | |
parent | 7c5c567ed5963b2ad2bfa6edcdbadf4fbbc6d4db (diff) | |
download | git-bug-cbff4b8aba9bc5207d07523181f2be8e94ece881.tar.gz |
add a way to load a bug from a prefix
Diffstat (limited to 'bug/bug.go')
-rw-r--r-- | bug/bug.go | 31 |
1 files changed, 31 insertions, 0 deletions
@@ -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) |