From 247a96f5b82c461e534f5a0ea1fe61f18a881617 Mon Sep 17 00:00:00 2001 From: John Cai Date: Sat, 10 Aug 2019 08:04:32 -0700 Subject: Port GetAllLFSPointers to go --- internal/git/catfile/objectinfo.go | 3 +++ internal/git/lfs.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 internal/git/lfs.go (limited to 'internal/git') diff --git a/internal/git/catfile/objectinfo.go b/internal/git/catfile/objectinfo.go index 7f7af80a5..de296287d 100644 --- a/internal/git/catfile/objectinfo.go +++ b/internal/git/catfile/objectinfo.go @@ -28,6 +28,9 @@ func (o *ObjectInfo) IsBlob() bool { return o.Type == "blob" } +// ParseObjectInfo reads from a reader and parses the data into an ObjectInfo struct +var ParseObjectInfo = parseObjectInfo + func parseObjectInfo(stdout *bufio.Reader) (*ObjectInfo, error) { infoLine, err := stdout.ReadString('\n') if err != nil { diff --git a/internal/git/lfs.go b/internal/git/lfs.go new file mode 100644 index 000000000..78f3aadd9 --- /dev/null +++ b/internal/git/lfs.go @@ -0,0 +1,31 @@ +package git + +import ( + "bytes" + "regexp" +) + +var ( + lfsOIDRe = regexp.MustCompile(`(?m)^oid sha256:[0-9a-f]{64}$`) + lfsSizeRe = regexp.MustCompile(`(?m)^size [0-9]+$`) +) + +// IsLFSPointer checks to see if a blob is an LFS pointer. It returns the raw data of the pointer if it is +func IsLFSPointer(b []byte) bool { + // ensure the version exists + if !bytes.HasPrefix(b, []byte("version https://git-lfs.github.com/spec")) { + return false + } + + // ensure the oid exists + if !lfsOIDRe.Match(b) { + return false + } + + // ensure the size exists + if !lfsSizeRe.Match(b) { + return false + } + + return true +} -- cgit v1.2.3