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>2021-01-18 16:32:28 +0300
committerPatrick Steinhardt <psteinhardt@gitlab.com>2021-01-19 16:59:15 +0300
commit678847f02239da3c0361c081c58aba3fdebc59ef (patch)
treee3e4384c701cfda5d2444ed304a2d056aaefdce8
parent83164e9ad9ad9b4608330d0e20b5c20b7f1cceae (diff)
git: Introduce new ObjectID type
We're currently passing around object IDs as plain strings, making it harder than necessary at times to see what a given thing actually represents. To lay the foundation, this commit introduces a new ObjectID which we can slowly migrate towards in the future to improve type safety.
-rw-r--r--internal/git/id.go24
-rw-r--r--internal/git/id_test.go49
2 files changed, 73 insertions, 0 deletions
diff --git a/internal/git/id.go b/internal/git/id.go
index be07c4d79..e64942c73 100644
--- a/internal/git/id.go
+++ b/internal/git/id.go
@@ -14,6 +14,30 @@ var (
objectIDRegex = regexp.MustCompile(`\A[0-9a-f]{40}\z`)
)
+// ObjectID represents an object ID.
+type ObjectID string
+
+// NewObjectIDFromHex constructs a new ObjectID from the given hex
+// 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
+}
+
+// String returns the hex representation of the ObjectID.
+func (oid ObjectID) String() string {
+ return string(oid)
+}
+
+// Revision returns a revision of the ObjectID. This directly returns the hex
+// representation as every object ID is a valid revision.
+func (oid ObjectID) Revision() Revision {
+ return Revision(oid.String())
+}
+
// ValidateObjectID checks if id is a syntactically correct object ID. Abbreviated
// object IDs are not deemed to be valid. Returns an ErrInvalidObjectID if the
// id is not valid.
diff --git a/internal/git/id_test.go b/internal/git/id_test.go
index 5bff91ac2..0ab6f0dd9 100644
--- a/internal/git/id_test.go
+++ b/internal/git/id_test.go
@@ -55,3 +55,52 @@ func TestValidateObjectID(t *testing.T) {
})
}
}
+
+func TestNewObjectIDFromHex(t *testing.T) {
+ for _, tc := range []struct {
+ desc string
+ oid string
+ valid bool
+ }{
+ {
+ desc: "valid object ID",
+ oid: "356e7793f9654d51dfb27312a1464062bceb9fa3",
+ valid: true,
+ },
+ {
+ desc: "object ID with non-hex characters fails",
+ oid: "x56e7793f9654d51dfb27312a1464062bceb9fa3",
+ valid: false,
+ },
+ {
+ desc: "object ID with upper-case letters fails",
+ oid: "356E7793F9654D51DFB27312A1464062BCEB9FA3",
+ valid: false,
+ },
+ {
+ desc: "too short object ID fails",
+ oid: "356e7793f9654d51dfb27312a1464062bceb9fa",
+ valid: false,
+ },
+ {
+ desc: "too long object ID fails",
+ oid: "356e7793f9654d51dfb27312a1464062bceb9fa33",
+ valid: false,
+ },
+ {
+ desc: "empty string fails",
+ oid: "",
+ valid: false,
+ },
+ } {
+ t.Run(tc.desc, func(t *testing.T) {
+ oid, err := NewObjectIDFromHex(tc.oid)
+ if tc.valid {
+ require.NoError(t, err)
+ require.Equal(t, tc.oid, oid.String())
+ } else {
+ require.Error(t, err)
+ }
+ })
+ }
+}