From 53b91ef306166d39dea3c70fb8ce14973c7ae111 Mon Sep 17 00:00:00 2001 From: ᴜɴᴋɴᴡᴏɴ Date: Sun, 5 Apr 2020 00:14:22 +0800 Subject: lfs: run e2e and fix minor issues (#6059) --- internal/lfsutil/oid.go | 23 +++++++---------------- internal/lfsutil/oid_test.go | 18 +++++------------- internal/lfsutil/storage.go | 5 +---- internal/lfsutil/storage_test.go | 2 +- 4 files changed, 14 insertions(+), 34 deletions(-) (limited to 'internal/lfsutil') diff --git a/internal/lfsutil/oid.go b/internal/lfsutil/oid.go index 899c7332..16d0ad3d 100644 --- a/internal/lfsutil/oid.go +++ b/internal/lfsutil/oid.go @@ -5,26 +5,17 @@ package lfsutil import ( - "strings" + "gogs.io/gogs/internal/lazyregexp" ) // OID is an LFS object ID. type OID string -// ValidOID returns true if given oid is valid according to spec: -// https://github.com/git-lfs/git-lfs/blob/master/docs/spec.md -func ValidOID(oid OID) bool { - fields := strings.SplitN(string(oid), ":", 2) - if len(fields) != 2 { - return false - } - method := fields[0] - hash := fields[1] +// An OID is a 64-char lower case hexadecimal, produced by SHA256. +// Spec: https://github.com/git-lfs/git-lfs/blob/master/docs/spec.md +var oidRe = lazyregexp.New("^[a-f0-9]{64}$") - switch method { - case "sha256": - // SHA256 produces 64-char lower case hexadecimal hash - return len(hash) == 64 && strings.ToLower(hash) == hash - } - return false +// ValidOID returns true if given oid is valid. +func ValidOID(oid OID) bool { + return oidRe.MatchString(string(oid)) } diff --git a/internal/lfsutil/oid_test.go b/internal/lfsutil/oid_test.go index 1e3b020a..1a447648 100644 --- a/internal/lfsutil/oid_test.go +++ b/internal/lfsutil/oid_test.go @@ -18,24 +18,16 @@ func TestValidOID(t *testing.T) { }{ { name: "malformed", - oid: OID("12345678"), + oid: OID("7c222fb2927d828af22f592134e8932480637c0d"), }, { - name: "unknown method", - oid: OID("sha1:7c222fb2927d828af22f592134e8932480637c0d"), + name: "not all lower cased", + oid: OID("EF797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f"), }, { - name: "sha256: malformed", - oid: OID("sha256:7c222fb2927d828af22f592134e8932480637c0d"), - }, - { - name: "sha256: not all lower cased", - oid: OID("sha256:EF797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f"), - }, - { - name: "sha256: valid", - oid: OID("sha256:ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f"), + name: "valid", + oid: OID("ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f"), expVal: true, }, } diff --git a/internal/lfsutil/storage.go b/internal/lfsutil/storage.go index fa10ee03..b2bfe37f 100644 --- a/internal/lfsutil/storage.go +++ b/internal/lfsutil/storage.go @@ -6,7 +6,6 @@ package lfsutil import ( "path/filepath" - "strings" ) // Storage is the storage type of an LFS object. @@ -23,7 +22,5 @@ func StorageLocalPath(root string, oid OID) string { return "" } - // Valid OID is guaranteed to have second element as hash. - hash := strings.SplitN(string(oid), ":", 2)[1] - return filepath.Join(root, string(hash[0]), string(hash[1]), hash) + return filepath.Join(root, string(oid[0]), string(oid[1]), string(oid)) } diff --git a/internal/lfsutil/storage_test.go b/internal/lfsutil/storage_test.go index 4f64b92b..bfb69e4a 100644 --- a/internal/lfsutil/storage_test.go +++ b/internal/lfsutil/storage_test.go @@ -31,7 +31,7 @@ func TestStorageLocalPath(t *testing.T) { { name: "valid oid", root: "/lfs-objects", - oid: OID("sha256:ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f"), + oid: OID("ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f"), expPath: "/lfs-objects/e/f/ef797c8118f02dfb649607dd5d3f8c7623048c9c063d532cc95c5ed7a898a64f", }, } -- cgit v1.2.3