aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2018-07-14 22:48:54 +0200
committerMichael Muré <batolettre@gmail.com>2018-07-14 22:48:54 +0200
commitfd4fa96c19dbb9b140726c48ff66d1f79646019a (patch)
treed8fa20de38e65b2716da9f62c2b1c4fef89e542c
parent81e15f07deaffaeabe8d30c9d92ccba5047071e1 (diff)
downloadgit-bug-fd4fa96c19dbb9b140726c48ff66d1f79646019a.tar.gz
bug: use a 40 char truncated sha256 hash as ID, 8 char for human reading
-rw-r--r--bug/bug.go28
1 files changed, 14 insertions, 14 deletions
diff --git a/bug/bug.go b/bug/bug.go
index 9803a970..d0afa830 100644
--- a/bug/bug.go
+++ b/bug/bug.go
@@ -1,6 +1,7 @@
package bug
import (
+ "crypto/sha256"
"errors"
"fmt"
"github.com/MichaelMure/git-bug/repository"
@@ -18,7 +19,7 @@ const RootEntryName = "root"
// used for merge of two different version.
type Bug struct {
// Id used as unique identifier
- id uuid.UUID
+ id string
lastCommit util.Hash
root util.Hash
@@ -33,15 +34,20 @@ type Bug struct {
// Create a new Bug
func NewBug() (*Bug, error) {
// Creating UUID Version 4
- id, err := uuid.ID4()
+ unique, err := uuid.ID4()
if err != nil {
return nil, err
}
+ // Use it as source of uniqueness
+ hash := sha256.New().Sum(unique.Bytes())
+
+ // format in hex and truncate to 40 char
+ id := fmt.Sprintf("%.40s", fmt.Sprintf("%x", hash))
+
return &Bug{
- id: id,
- lastCommit: "",
+ id: id,
}, nil
}
@@ -53,14 +59,8 @@ func ReadBug(repo repository.Repo, id string) (*Bug, error) {
return nil, err
}
- parsedId, err := uuid.FromString(id)
-
- if err != nil {
- return nil, err
- }
-
bug := Bug{
- id: parsedId,
+ id: id,
}
for _, hash := range hashes {
@@ -207,7 +207,7 @@ func (bug *Bug) Commit(repo repository.Repo) error {
bug.lastCommit = hash
// Create or update the Git reference for this bug
- ref := fmt.Sprintf("%s%s", BugsRefPattern, bug.id.String())
+ ref := fmt.Sprintf("%s%s", BugsRefPattern, bug.id)
err = repo.UpdateRef(ref, hash)
if err != nil {
@@ -221,11 +221,11 @@ func (bug *Bug) Commit(repo repository.Repo) error {
}
func (bug *Bug) Id() string {
- return fmt.Sprintf("%x", bug.id)
+ return bug.id
}
func (bug *Bug) HumanId() string {
- return fmt.Sprintf("%.8s", bug.Id())
+ return fmt.Sprintf("%.8s", bug.id)
}
func (bug *Bug) firstOp() Operation {