1package lfs23import (4 "crypto/sha256"5 "encoding/hex"6 "errors"7 "fmt"8 "io"9 "path"10 "regexp"11 "strconv"12 "strings"13)1415const (16 blobSizeCutoff = 10241718 // HashAlgorithmSHA256 is the hash algorithm used for Git LFS.19 HashAlgorithmSHA256 = "sha256"2021 // MetaFileIdentifier is the string appearing at the first line of LFS pointer files.22 // https://github.com/git-lfs/git-lfs/blob/master/docs/spec.md23 MetaFileIdentifier = "version https://git-lfs.github.com/spec/v1"2425 // MetaFileOidPrefix appears in LFS pointer files on a line before the sha256 hash.26 MetaFileOidPrefix = "oid " + HashAlgorithmSHA256 + ":"27)2829var (30 // ErrMissingPrefix occurs if the content lacks the LFS prefix31 ErrMissingPrefix = errors.New("Content lacks the LFS prefix")3233 // ErrInvalidStructure occurs if the content has an invalid structure34 ErrInvalidStructure = errors.New("Content has an invalid structure")3536 // ErrInvalidOIDFormat occurs if the oid has an invalid format37 ErrInvalidOIDFormat = errors.New("OID has an invalid format")38)3940// ReadPointer tries to read LFS pointer data from the reader41func ReadPointer(reader io.Reader) (Pointer, error) {42 buf := make([]byte, blobSizeCutoff)43 n, err := io.ReadFull(reader, buf)44 if err != nil && err != io.ErrUnexpectedEOF {45 return Pointer{}, err46 }47 buf = buf[:n]4849 return ReadPointerFromBuffer(buf)50}5152var oidPattern = regexp.MustCompile(`^[a-f\d]{64}$`)5354// ReadPointerFromBuffer will return a pointer if the provided byte slice is a pointer file or an error otherwise.55func ReadPointerFromBuffer(buf []byte) (Pointer, error) {56 var p Pointer5758 headString := string(buf)59 if !strings.HasPrefix(headString, MetaFileIdentifier) {60 return p, ErrMissingPrefix61 }6263 splitLines := strings.Split(headString, "\n")64 if len(splitLines) < 3 {65 return p, ErrInvalidStructure66 }6768 oid := strings.TrimPrefix(splitLines[1], MetaFileOidPrefix)69 if len(oid) != 64 || !oidPattern.MatchString(oid) {70 return p, ErrInvalidOIDFormat71 }72 size, err := strconv.ParseInt(strings.TrimPrefix(splitLines[2], "size "), 10, 64)73 if err != nil {74 return p, err75 }7677 p.Oid = oid78 p.Size = size7980 return p, nil81}8283// IsValid checks if the pointer has a valid structure.84// It doesn't check if the pointed-to-content exists.85func (p Pointer) IsValid() bool {86 if len(p.Oid) != 64 {87 return false88 }89 if !oidPattern.MatchString(p.Oid) {90 return false91 }92 if p.Size < 0 {93 return false94 }95 return true96}9798// String returns the string representation of the pointer99// https://github.com/git-lfs/git-lfs/blob/main/docs/spec.md#the-pointer100func (p Pointer) String() string {101 return fmt.Sprintf("%s\n%s%s\nsize %d\n", MetaFileIdentifier, MetaFileOidPrefix, p.Oid, p.Size)102}103104// RelativePath returns the relative storage path of the pointer105// https://github.com/git-lfs/git-lfs/blob/main/docs/spec.md#intercepting-git106func (p Pointer) RelativePath() string {107 if len(p.Oid) < 5 {108 return p.Oid109 }110111 return path.Join(p.Oid[0:2], p.Oid[2:4], p.Oid)112}113114// GeneratePointer generates a pointer for arbitrary content115func GeneratePointer(content io.Reader) (Pointer, error) {116 h := sha256.New()117 c, err := io.Copy(h, content)118 if err != nil {119 return Pointer{}, err120 }121 sum := h.Sum(nil)122 return Pointer{Oid: hex.EncodeToString(sum), Size: c}, nil123}