2014-09-23 20:39:12 +00:00
|
|
|
package backend
|
2014-04-21 21:25:31 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
2014-09-23 20:39:12 +00:00
|
|
|
"errors"
|
2014-04-21 21:25:31 +00:00
|
|
|
)
|
|
|
|
|
2014-11-23 15:48:00 +00:00
|
|
|
// IDSize contains the size of an ID, in bytes.
|
2015-02-11 18:25:43 +00:00
|
|
|
const IDSize = hashSize
|
2014-09-23 20:39:12 +00:00
|
|
|
|
2015-05-02 14:22:43 +00:00
|
|
|
// ID references content within a repository.
|
2015-07-25 15:05:45 +00:00
|
|
|
type ID [IDSize]byte
|
2014-04-21 21:25:31 +00:00
|
|
|
|
|
|
|
// ParseID converts the given string to an ID.
|
|
|
|
func ParseID(s string) (ID, error) {
|
|
|
|
b, err := hex.DecodeString(s)
|
|
|
|
|
|
|
|
if err != nil {
|
2015-07-25 15:05:45 +00:00
|
|
|
return ID{}, err
|
2014-04-21 21:25:31 +00:00
|
|
|
}
|
|
|
|
|
2014-11-23 15:48:00 +00:00
|
|
|
if len(b) != IDSize {
|
2015-07-25 15:05:45 +00:00
|
|
|
return ID{}, errors.New("invalid length for hash")
|
2014-09-23 20:39:12 +00:00
|
|
|
}
|
|
|
|
|
2015-07-25 15:05:45 +00:00
|
|
|
id := ID{}
|
|
|
|
copy(id[:], b)
|
|
|
|
|
|
|
|
return id, nil
|
2014-04-21 21:25:31 +00:00
|
|
|
}
|
2014-07-28 18:20:32 +00:00
|
|
|
|
2014-04-21 21:25:31 +00:00
|
|
|
func (id ID) String() string {
|
2015-07-25 15:05:45 +00:00
|
|
|
return hex.EncodeToString(id[:])
|
2014-04-21 21:25:31 +00:00
|
|
|
}
|
|
|
|
|
2015-01-10 22:40:10 +00:00
|
|
|
const shortStr = 4
|
|
|
|
|
2015-07-25 15:05:45 +00:00
|
|
|
// Str returns the shortened string version of id.
|
|
|
|
func (id *ID) Str() string {
|
2015-03-15 18:05:25 +00:00
|
|
|
if id == nil {
|
|
|
|
return "[nil]"
|
|
|
|
}
|
2015-07-25 15:05:45 +00:00
|
|
|
|
|
|
|
if id.IsNull() {
|
|
|
|
return "[null]"
|
|
|
|
}
|
|
|
|
|
2015-01-10 22:40:10 +00:00
|
|
|
return hex.EncodeToString(id[:shortStr])
|
|
|
|
}
|
|
|
|
|
2015-07-25 15:05:45 +00:00
|
|
|
// IsNull returns true iff id only consists of null bytes.
|
|
|
|
func (id ID) IsNull() bool {
|
|
|
|
var nullID ID
|
|
|
|
|
|
|
|
return id == nullID
|
|
|
|
}
|
|
|
|
|
2014-04-21 21:25:31 +00:00
|
|
|
// Equal compares an ID to another other.
|
|
|
|
func (id ID) Equal(other ID) bool {
|
2015-07-25 15:05:45 +00:00
|
|
|
return id == other
|
2014-04-21 21:25:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// EqualString compares this ID to another one, given as a string.
|
|
|
|
func (id ID) EqualString(other string) (bool, error) {
|
|
|
|
s, err := hex.DecodeString(other)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2015-07-25 15:05:45 +00:00
|
|
|
id2 := ID{}
|
|
|
|
copy(id2[:], s)
|
|
|
|
|
|
|
|
return id == id2, nil
|
2014-04-21 21:25:31 +00:00
|
|
|
}
|
|
|
|
|
2014-11-21 20:21:44 +00:00
|
|
|
// Compare compares this ID to another one, returning -1, 0, or 1.
|
|
|
|
func (id ID) Compare(other ID) int {
|
2015-07-25 15:05:45 +00:00
|
|
|
return bytes.Compare(other[:], id[:])
|
2014-11-21 20:21:44 +00:00
|
|
|
}
|
|
|
|
|
2014-04-21 21:25:31 +00:00
|
|
|
func (id ID) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(id.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (id *ID) UnmarshalJSON(b []byte) error {
|
|
|
|
var s string
|
|
|
|
err := json.Unmarshal(b, &s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-07-25 15:05:45 +00:00
|
|
|
_, err = hex.Decode(id[:], []byte(s))
|
2014-04-21 21:25:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2014-09-21 14:33:20 +00:00
|
|
|
|
|
|
|
func IDFromData(d []byte) ID {
|
2015-07-25 15:05:45 +00:00
|
|
|
return hashData(d)
|
2014-09-21 14:33:20 +00:00
|
|
|
}
|