diff options
author | Michael Muré <batolettre@gmail.com> | 2021-04-04 15:49:16 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-04 15:49:16 +0200 |
commit | fc04af34f5d6dff930003cc1f27ead972d424324 (patch) | |
tree | 8b9750d46182fff88d72fcc891e89d5a79ed83b0 /entity/id.go | |
parent | 2055bd5d0afb534cc98cd11bd9802d66acbb0c8f (diff) | |
parent | cb9b06551ddc1fae33046733f79ede20f8d09f9a (diff) | |
download | git-bug-fc04af34f5d6dff930003cc1f27ead972d424324.tar.gz |
Merge pull request #532 from MichaelMure/dag-entity
Work towards a reusable entity datastructure + commit signature
Diffstat (limited to 'entity/id.go')
-rw-r--r-- | entity/id.go | 20 |
1 files changed, 17 insertions, 3 deletions
diff --git a/entity/id.go b/entity/id.go index 1b78aacd..c8dbdb94 100644 --- a/entity/id.go +++ b/entity/id.go @@ -1,6 +1,7 @@ package entity import ( + "crypto/sha256" "fmt" "io" "strings" @@ -8,8 +9,8 @@ import ( "github.com/pkg/errors" ) -const IdLengthSHA1 = 40 -const IdLengthSHA256 = 64 +// sha-256 +const idLength = 64 const humanIdLength = 7 const UnsetId = Id("unset") @@ -17,6 +18,15 @@ const UnsetId = Id("unset") // Id is an identifier for an entity or part of an entity type Id string +// DeriveId generate an Id from the serialization of the object or part of the object. +func DeriveId(data []byte) Id { + // My understanding is that sha256 is enough to prevent collision (git use that, so ...?) + // If you read this code, I'd be happy to be schooled. + + sum := sha256.Sum256(data) + return Id(fmt.Sprintf("%x", sum)) +} + // String return the identifier as a string func (i Id) String() string { return string(i) @@ -55,7 +65,11 @@ func (i Id) MarshalGQL(w io.Writer) { // IsValid tell if the Id is valid func (i Id) Validate() error { - if len(i) != IdLengthSHA1 && len(i) != IdLengthSHA256 { + // Special case to detect outdated repo + if len(i) == 40 { + return fmt.Errorf("outdated repository format, please use https://github.com/MichaelMure/git-bug-migration to upgrade") + } + if len(i) != idLength { return fmt.Errorf("invalid length") } for _, r := range i { |