1package lfs
2
3import (
4 "crypto/sha256"
5 "encoding/hex"
6 "errors"
7 "fmt"
8 "io"
9 "path"
10 "regexp"
11 "strconv"
12 "strings"
13)
14
15const (
16 blobSizeCutoff = 1024
17 hashAlgo = "sha256"
18
19 // MetaFileIdentifier is the string appearing at the first line of LFS pointer files.
20 // https://github.com/git-lfs/git-lfs/blob/master/docs/spec.md
21 MetaFileIdentifier = "version https://git-lfs.github.com/spec/v1"
22
23 // MetaFileOidPrefix appears in LFS pointer files on a line before the sha256 hash.
24 MetaFileOidPrefix = "oid " + hashAlgo + ":"
25)
26
27var (
28 // ErrMissingPrefix occurs if the content lacks the LFS prefix
29 ErrMissingPrefix = errors.New("Content lacks the LFS prefix")
30
31 // ErrInvalidStructure occurs if the content has an invalid structure
32 ErrInvalidStructure = errors.New("Content has an invalid structure")
33
34 // ErrInvalidOIDFormat occurs if the oid has an invalid format
35 ErrInvalidOIDFormat = errors.New("OID has an invalid format")
36)
37
38// ReadPointer tries to read LFS pointer data from the reader
39func ReadPointer(reader io.Reader) (Pointer, error) {
40 buf := make([]byte, blobSizeCutoff)
41 n, err := io.ReadFull(reader, buf)
42 if err != nil && err != io.ErrUnexpectedEOF {
43 return Pointer{}, err
44 }
45 buf = buf[:n]
46
47 return ReadPointerFromBuffer(buf)
48}
49
50var oidPattern = regexp.MustCompile(`^[a-f\d]{64}$`)
51
52// ReadPointerFromBuffer will return a pointer if the provided byte slice is a pointer file or an error otherwise.
53func ReadPointerFromBuffer(buf []byte) (Pointer, error) {
54 var p Pointer
55
56 headString := string(buf)
57 if !strings.HasPrefix(headString, MetaFileIdentifier) {
58 return p, ErrMissingPrefix
59 }
60
61 splitLines := strings.Split(headString, "\n")
62 if len(splitLines) < 3 {
63 return p, ErrInvalidStructure
64 }
65
66 oid := strings.TrimPrefix(splitLines[1], MetaFileOidPrefix)
67 if len(oid) != 64 || !oidPattern.MatchString(oid) {
68 return p, ErrInvalidOIDFormat
69 }
70 size, err := strconv.ParseInt(strings.TrimPrefix(splitLines[2], "size "), 10, 64)
71 if err != nil {
72 return p, err
73 }
74
75 p.Oid = oid
76 p.Size = size
77
78 return p, nil
79}
80
81// IsValid checks if the pointer has a valid structure.
82// It doesn't check if the pointed-to-content exists.
83func (p Pointer) IsValid() bool {
84 if len(p.Oid) != 64 {
85 return false
86 }
87 if !oidPattern.MatchString(p.Oid) {
88 return false
89 }
90 if p.Size < 0 {
91 return false
92 }
93 return true
94}
95
96// String returns the string representation of the pointer
97// https://github.com/git-lfs/git-lfs/blob/main/docs/spec.md#the-pointer
98func (p Pointer) String() string {
99 return fmt.Sprintf("%s\n%s%s\nsize %d\n", MetaFileIdentifier, MetaFileOidPrefix, p.Oid, p.Size)
100}
101
102// RelativePath returns the relative storage path of the pointer
103func (p Pointer) RelativePath() string {
104 if len(p.Oid) < 5 {
105 return p.Oid
106 }
107
108 return path.Join(p.Oid[0:2], p.Oid[2:4], p.Oid[4:])
109}
110
111// GeneratePointer generates a pointer for arbitrary content
112func GeneratePointer(content io.Reader) (Pointer, error) {
113 h := sha256.New()
114 c, err := io.Copy(h, content)
115 if err != nil {
116 return Pointer{}, err
117 }
118 sum := h.Sum(nil)
119 return Pointer{Oid: hex.EncodeToString(sum), Size: c}, nil
120}