Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Steinhardt <psteinhardt@gitlab.com>2022-07-18 12:47:01 +0300
committerPatrick Steinhardt <psteinhardt@gitlab.com>2022-07-20 07:36:37 +0300
commit0c00b8999ad57f86a8884a6460f9b1e631760038 (patch)
tree28654a9d0d788a2ddd2ca349573e3a9b46cf934d
parent7659b85ec0f715588f9578832d50effcf3893996 (diff)
git: Introduce infra to encapsulate different object ID implementations
Introduce the infrastructure to encapsulate different object ID implementations in the form of a new `ObjectHash` structure. This structure is supposed to host all information required to properly handle object IDs regardless of whether one is using SHA1 or SHA256 as a hash function.
-rw-r--r--internal/git/object_id.go48
1 files changed, 37 insertions, 11 deletions
diff --git a/internal/git/object_id.go b/internal/git/object_id.go
index d4cb725dd..d47efc7c8 100644
--- a/internal/git/object_id.go
+++ b/internal/git/object_id.go
@@ -16,13 +16,46 @@ const (
)
var (
+ // ObjectHashSHA1 is the implementation of an object ID via SHA1.
+ ObjectHashSHA1 = ObjectHash{
+ regexp: regexp.MustCompile(`\A[0-9a-f]{40}\z`),
+ }
+
+ // ObjectHashSHA256 is the implementation of an object ID via SHA256.
+ ObjectHashSHA256 = ObjectHash{
+ regexp: regexp.MustCompile(`\A[0-9a-f]{64}\z`),
+ }
+
// ErrInvalidObjectID is returned in case an object ID's string
// representation is not a valid one.
ErrInvalidObjectID = errors.New("invalid object ID")
-
- objectIDRegex = regexp.MustCompile(`\A[0-9a-f]{40}\z`)
)
+// ObjectHash is a hash-function specific implementation of an object ID.
+type ObjectHash struct {
+ regexp *regexp.Regexp
+}
+
+// FromHex constructs a new ObjectID from the given hex representation of the object ID. Returns
+// ErrInvalidObjectID if the given object ID is not valid.
+func (h ObjectHash) FromHex(hex string) (ObjectID, error) {
+ if err := h.ValidateHex(hex); err != nil {
+ return "", err
+ }
+
+ return ObjectID(hex), nil
+}
+
+// ValidateHex checks if `hex` is a syntactically correct object ID for the given hash. Abbreviated
+// object IDs are not deemed to be valid. Returns an `ErrInvalidObjectID` if the `hex` is not valid.
+func (h ObjectHash) ValidateHex(hex string) error {
+ if h.regexp.MatchString(hex) {
+ return nil
+ }
+
+ return fmt.Errorf("%w: %q", ErrInvalidObjectID, hex)
+}
+
// ObjectID represents an object ID.
type ObjectID string
@@ -30,10 +63,7 @@ type ObjectID string
// representation of the object ID. Returns ErrInvalidObjectID if the given
// OID is not valid.
func NewObjectIDFromHex(hex string) (ObjectID, error) {
- if err := ValidateObjectID(hex); err != nil {
- return "", err
- }
- return ObjectID(hex), nil
+ return ObjectHashSHA1.FromHex(hex)
}
// String returns the hex representation of the ObjectID.
@@ -60,11 +90,7 @@ func (oid ObjectID) Revision() Revision {
// object IDs are not deemed to be valid. Returns an ErrInvalidObjectID if the
// id is not valid.
func ValidateObjectID(id string) error {
- if objectIDRegex.MatchString(id) {
- return nil
- }
-
- return fmt.Errorf("%w: %q", ErrInvalidObjectID, id)
+ return ObjectHashSHA1.ValidateHex(id)
}
// IsZeroOID is a shortcut for `something == git.ZeroOID.String()`