Skip to content

Commit c007e31

Browse files
committed
packfiles: use go-git DotGit to manage packfiles
Signed-off-by: Miguel Molina <miguel@erizocosmi.co>
1 parent dd0d9e1 commit c007e31

File tree

1 file changed

+22
-46
lines changed

1 file changed

+22
-46
lines changed

packfiles.go

Lines changed: 22 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ import (
66
"io/ioutil"
77
"os"
88
"path/filepath"
9-
"strings"
109

1110
errors "gopkg.in/src-d/go-errors.v1"
1211
"gopkg.in/src-d/go-git.v4/plumbing/object"
1312
"gopkg.in/src-d/go-git.v4/plumbing/storer"
1413
"gopkg.in/src-d/go-git.v4/storage/filesystem"
14+
"gopkg.in/src-d/go-git.v4/storage/filesystem/dotgit"
1515

1616
"gopkg.in/src-d/go-billy-siva.v4"
1717
billy "gopkg.in/src-d/go-billy.v4"
@@ -25,7 +25,7 @@ type packRepository struct {
2525
packs map[plumbing.Hash]packfile.Index
2626
}
2727

28-
func repositoryPackfiles(path string, kind repoKind) (billy.Filesystem, []plumbing.Hash, error) {
28+
func repositoryPackfiles(path string, kind repoKind) (*dotgit.DotGit, []plumbing.Hash, error) {
2929
fs, err := repoFilesystem(path, kind)
3030
if err != nil {
3131
return nil, nil, err
@@ -36,8 +36,13 @@ func repositoryPackfiles(path string, kind repoKind) (billy.Filesystem, []plumbi
3636
return nil, nil, err
3737
}
3838

39-
packfiles, err := findPackfiles(fs)
40-
return fs, packfiles, err
39+
dot := dotgit.New(fs)
40+
packfiles, err := dot.ObjectPacks()
41+
if err != nil {
42+
return nil, nil, err
43+
}
44+
45+
return dot, packfiles, nil
4146
}
4247

4348
type packfileIndex struct {
@@ -48,14 +53,14 @@ type packfileIndex struct {
4853
type repositoryIndex []*packfileIndex
4954

5055
func newRepositoryIndex(path string, kind repoKind) (*repositoryIndex, error) {
51-
fs, packfiles, err := repositoryPackfiles(path, kind)
56+
dot, packfiles, err := repositoryPackfiles(path, kind)
5257
if err != nil {
5358
return nil, err
5459
}
5560

5661
var result repositoryIndex
5762
for _, p := range packfiles {
58-
idx, err := openPackfileIndex(fs, path, p)
63+
idx, err := openPackfileIndex(dot, p)
5964
if err != nil {
6065
return nil, err
6166
}
@@ -67,12 +72,10 @@ func newRepositoryIndex(path string, kind repoKind) (*repositoryIndex, error) {
6772
}
6873

6974
func openPackfileIndex(
70-
fs billy.Filesystem,
71-
path string,
75+
dotGit *dotgit.DotGit,
7276
hash plumbing.Hash,
7377
) (*packfile.Index, error) {
74-
path = fs.Join("objects", "pack", fmt.Sprintf("pack-%s.idx", hash))
75-
f, err := fs.Open(path)
78+
f, err := dotGit.ObjectPackIdx(hash)
7679
if err != nil {
7780
return nil, err
7881
}
@@ -127,32 +130,6 @@ func findDotGit(fs billy.Filesystem) (billy.Filesystem, error) {
127130
return fs, nil
128131
}
129132

130-
func findPackfiles(fs billy.Filesystem) ([]plumbing.Hash, error) {
131-
packDir := fs.Join("objects", "pack")
132-
files, err := fs.ReadDir(packDir)
133-
if err != nil {
134-
if os.IsNotExist(err) {
135-
return nil, nil
136-
}
137-
138-
return nil, err
139-
}
140-
141-
var packs []plumbing.Hash
142-
for _, f := range files {
143-
if !strings.HasSuffix(f.Name(), ".pack") {
144-
continue
145-
}
146-
147-
n := f.Name()
148-
h := plumbing.NewHash(n[5 : len(n)-5]) //pack-(hash).pack
149-
packs = append(packs, h)
150-
151-
}
152-
153-
return packs, nil
154-
}
155-
156133
type objectIter struct {
157134
packs *packIter
158135
packObjects *packObjectIter
@@ -219,7 +196,7 @@ type packIter struct {
219196
repo *repository
220197

221198
storage storer.EncodedObjectStorer
222-
fs billy.Filesystem
199+
dotGit *dotgit.DotGit
223200
packfiles []plumbing.Hash
224201
packpos int
225202
}
@@ -242,15 +219,17 @@ func (i *packIter) Next() (*packObjectIter, error) {
242219

243220
if len(i.packfiles) == 0 {
244221
var err error
245-
i.fs, i.packfiles, err = repositoryPackfiles(i.repo.path, i.repo.kind)
222+
i.dotGit, i.packfiles, err = repositoryPackfiles(i.repo.path, i.repo.kind)
246223
if err != nil {
247224
return nil, err
248225
}
249226
i.packpos = 0
250-
i.storage, err = filesystem.NewStorage(i.fs)
227+
228+
storage, err := filesystem.NewObjectStorage(i.dotGit)
251229
if err != nil {
252230
return nil, err
253231
}
232+
i.storage = &storage
254233
}
255234

256235
if i.packpos >= len(i.packfiles) {
@@ -262,7 +241,7 @@ func (i *packIter) Next() (*packObjectIter, error) {
262241
pf := i.packfiles[i.packpos]
263242
i.packpos++
264243

265-
return newPackObjectIter(i.repo.path, i.fs, pf, i.storage, i.typ)
244+
return newPackObjectIter(i.repo.path, i.dotGit, pf, i.storage, i.typ)
266245
}
267246
}
268247

@@ -278,20 +257,17 @@ type packObjectIter struct {
278257

279258
func newPackObjectIter(
280259
path string,
281-
fs billy.Filesystem,
260+
dotGit *dotgit.DotGit,
282261
hash plumbing.Hash,
283262
storage storer.EncodedObjectStorer,
284263
typ plumbing.ObjectType,
285264
) (*packObjectIter, error) {
286-
packfilePath := fs.Join("objects", "pack", fmt.Sprintf("pack-%s.pack", hash))
287-
idxfilePath := fs.Join("objects", "pack", fmt.Sprintf("pack-%s.idx", hash))
288-
289-
packf, err := fs.Open(packfilePath)
265+
packf, err := dotGit.ObjectPack(hash)
290266
if err != nil {
291267
return nil, err
292268
}
293269

294-
idxf, err := fs.Open(idxfilePath)
270+
idxf, err := dotGit.ObjectPackIdx(hash)
295271
if err != nil {
296272
return nil, err
297273
}

0 commit comments

Comments
 (0)