| 1 |
package pack |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"hash" |
| 6 |
"os" |
| 7 |
"path/filepath" |
| 8 |
"regexp" |
| 9 |
"sort" |
| 10 |
"strings" |
| 11 |
|
| 12 |
"github.com/git-lfs/gitobj/v2/errors" |
| 13 |
) |
| 14 |
|
| 15 |
// Set allows access of objects stored across a set of packfiles. |
| 16 |
type Set struct { |
| 17 |
// m maps the leading byte of a SHA-1 object name to a set of packfiles |
| 18 |
// that might contain that object, in order of which packfile is most |
| 19 |
// likely to contain that object. |
| 20 |
m map[byte][]*Packfile |
| 21 |
|
| 22 |
// closeFn is a function that is run by Close(), designated to free |
| 23 |
// resources held by the *Set, like open packfiles. |
| 24 |
closeFn func() error |
| 25 |
} |
| 26 |
|
| 27 |
var ( |
| 28 |
// nameRe is a regular expression that matches the basename of a |
| 29 |
// filepath that is a packfile. |
| 30 |
// |
| 31 |
// It includes one matchgroup, which is the SHA-1 name of the pack. |
| 32 |
nameRe = regexp.MustCompile(`^(.*)\.pack$`) |
| 33 |
) |
| 34 |
|
| 35 |
// NewSet creates a new *Set of all packfiles found in a given object database's |
| 36 |
// root (i.e., "/path/to/repo/.git/objects"). |
| 37 |
// |
| 38 |
// It finds all packfiles in the "pack" subdirectory, and instantiates a *Set |
| 39 |
// containing them. If there was an error parsing the packfiles in that |
| 40 |
// directory, or the directory was otherwise unable to be observed, NewSet |
| 41 |
// returns that error. |
| 42 |
func NewSet(db string, algo hash.Hash) (*Set, error) { |
| 43 |
pd := filepath.Join(db, "pack") |
| 44 |
|
| 45 |
paths, err := filepath.Glob(filepath.Join(escapeGlobPattern(pd), "*.pack")) |
| 46 |
if err != nil { |
| 47 |
return nil, err |
| 48 |
} |
| 49 |
|
| 50 |
packs := make([]*Packfile, 0, len(paths)) |
| 51 |
|
| 52 |
for _, path := range paths { |
| 53 |
submatch := nameRe.FindStringSubmatch(filepath.Base(path)) |
| 54 |
if len(submatch) != 2 { |
| 55 |
continue |
| 56 |
} |
| 57 |
|
| 58 |
name := submatch[1] |
| 59 |
|
| 60 |
idxf, err := os.Open(filepath.Join(pd, fmt.Sprintf("%s.idx", name))) |
| 61 |
if err != nil { |
| 62 |
// We have a pack (since it matched the regex), but the |
| 63 |
// index is missing or unusable. Skip this pack and |
| 64 |
// continue on with the next one, as Git does. |
| 65 |
if idxf != nil { |
| 66 |
// In the unlikely event that we did open a |
| 67 |
// file, close it, but discard any error in |
| 68 |
// doing so. |
| 69 |
idxf.Close() |
| 70 |
} |
| 71 |
continue |
| 72 |
} |
| 73 |
|
| 74 |
packf, err := os.Open(filepath.Join(pd, fmt.Sprintf("%s.pack", name))) |
| 75 |
if err != nil { |
| 76 |
return nil, err |
| 77 |
} |
| 78 |
|
| 79 |
pack, err := DecodePackfile(packf, algo) |
| 80 |
if err != nil { |
| 81 |
return nil, err |
| 82 |
} |
| 83 |
|
| 84 |
idx, err := DecodeIndex(idxf, algo) |
| 85 |
if err != nil { |
| 86 |
return nil, err |
| 87 |
} |
| 88 |
|
| 89 |
pack.idx = idx |
| 90 |
|
| 91 |
packs = append(packs, pack) |
| 92 |
} |
| 93 |
return NewSetPacks(packs...), nil |
| 94 |
} |
| 95 |
|
| 96 |
// globEscapes uses these escapes because filepath.Glob does not understand |
| 97 |
// backslash escapes on Windows. |
| 98 |
var globEscapes = map[string]string{ |
| 99 |
"*": "[*]", |
| 100 |
"?": "[?]", |
| 101 |
"[": "[[]", |
| 102 |
} |
| 103 |
|
| 104 |
func escapeGlobPattern(s string) string { |
| 105 |
for char, escape := range globEscapes { |
| 106 |
s = strings.Replace(s, char, escape, -1) |
| 107 |
} |
| 108 |
return s |
| 109 |
} |
| 110 |
|
| 111 |
// NewSetPacks creates a new *Set from the given packfiles. |
| 112 |
func NewSetPacks(packs ...*Packfile) *Set { |
| 113 |
m := make(map[byte][]*Packfile) |
| 114 |
|
| 115 |
for i := 0; i < 256; i++ { |
| 116 |
n := byte(i) |
| 117 |
|
| 118 |
for j := 0; j < len(packs); j++ { |
| 119 |
pack := packs[j] |
| 120 |
|
| 121 |
var count uint32 |
| 122 |
if n == 0 { |
| 123 |
count = pack.idx.fanout[n] |
| 124 |
} else { |
| 125 |
count = pack.idx.fanout[n] - pack.idx.fanout[n-1] |
| 126 |
} |
| 127 |
|
| 128 |
if count > 0 { |
| 129 |
m[n] = append(m[n], pack) |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
sort.Slice(m[n], func(i, j int) bool { |
| 134 |
ni := m[n][i].idx.fanout[n] |
| 135 |
nj := m[n][j].idx.fanout[n] |
| 136 |
|
| 137 |
return ni > nj |
| 138 |
}) |
| 139 |
} |
| 140 |
|
| 141 |
return &Set{ |
| 142 |
m: m, |
| 143 |
closeFn: func() error { |
| 144 |
for _, pack := range packs { |
| 145 |
if err := pack.Close(); err != nil { |
| 146 |
return err |
| 147 |
} |
| 148 |
} |
| 149 |
return nil |
| 150 |
}, |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
// Close closes all open packfiles, returning an error if one was encountered. |
| 155 |
func (s *Set) Close() error { |
| 156 |
if s.closeFn == nil { |
| 157 |
return nil |
| 158 |
} |
| 159 |
return s.closeFn() |
| 160 |
} |
| 161 |
|
| 162 |
// Object opens (but does not unpack, or, apply the delta-base chain) a given |
| 163 |
// object in the first packfile that matches it. |
| 164 |
// |
| 165 |
// Object searches packfiles contained in the set in order of how many objects |
| 166 |
// they have that begin with the first by of the given SHA-1 "name", in |
| 167 |
// descending order. |
| 168 |
// |
| 169 |
// If the object was unable to be found in any of the packfiles, (nil, |
| 170 |
// ErrNotFound) will be returned. |
| 171 |
// |
| 172 |
// If there was otherwise an error opening the object for reading from any of |
| 173 |
// the packfiles, it will be returned, and no other packfiles will be searched. |
| 174 |
// |
| 175 |
// Otherwise, the object will be returned without error. |
| 176 |
func (s *Set) Object(name []byte) (*Object, error) { |
| 177 |
return s.each(name, func(p *Packfile) (*Object, error) { |
| 178 |
return p.Object(name) |
| 179 |
}) |
| 180 |
} |
| 181 |
|
| 182 |
// iterFn is a function that takes a given packfile and opens an object from it. |
| 183 |
type iterFn func(p *Packfile) (o *Object, err error) |
| 184 |
|
| 185 |
// each executes the given iterFn "fn" on each Packfile that has any objects |
| 186 |
// beginning with a prefix of the SHA-1 "name", in order of which packfiles have |
| 187 |
// the most objects beginning with that prefix. |
| 188 |
// |
| 189 |
// If any invocation of "fn" returns a non-nil error, it will either be a) |
| 190 |
// returned immediately, if the error is not ErrIsNotFound, or b) continued |
| 191 |
// immediately, if the error is ErrNotFound. |
| 192 |
// |
| 193 |
// If no packfiles match the given file, return errors.NoSuchObject, along with |
| 194 |
// no object. |
| 195 |
func (s *Set) each(name []byte, fn iterFn) (*Object, error) { |
| 196 |
var key byte |
| 197 |
if len(name) > 0 { |
| 198 |
key = name[0] |
| 199 |
} |
| 200 |
|
| 201 |
for _, pack := range s.m[key] { |
| 202 |
o, err := fn(pack) |
| 203 |
if err != nil { |
| 204 |
if IsNotFound(err) { |
| 205 |
continue |
| 206 |
} |
| 207 |
return nil, err |
| 208 |
} |
| 209 |
return o, nil |
| 210 |
} |
| 211 |
|
| 212 |
return nil, errors.NoSuchObject(name) |
| 213 |
} |