@@ -11,7 +11,9 @@ import (
11
11
"time"
12
12
13
13
"gopkg.in/src-d/go-git.v4/core"
14
- "gopkg.in/src-d/go-git.v4/storage/filesystem/internal/index"
14
+ "gopkg.in/src-d/go-git.v4/formats/idxfile"
15
+ "gopkg.in/src-d/go-git.v4/formats/packfile"
16
+ "gopkg.in/src-d/go-git.v4/storage/memory"
15
17
"gopkg.in/src-d/go-git.v4/utils/fs"
16
18
)
17
19
34
36
ErrIdxNotFound = errors .New ("idx file not found" )
35
37
// ErrPackfileNotFound is returned by Packfile when the packfile is not found
36
38
ErrPackfileNotFound = errors .New ("packfile not found" )
37
- // ErrObjfileNotFound is returned by Objectfile when the objectffile is not found
38
- ErrObjfileNotFound = errors .New ("object file not found" )
39
39
// ErrConfigNotFound is returned by Config when the config is not found
40
40
ErrConfigNotFound = errors .New ("config file not found" )
41
41
)
@@ -77,12 +77,14 @@ func (d *DotGit) Refs() ([]*core.Reference, error) {
77
77
return refs , nil
78
78
}
79
79
80
+ // NewObjectPack return a writer for a new packfile, it saves the packfile to
81
+ // disk and also generates and save the index for the given packfile.
80
82
func (d * DotGit ) NewObjectPack () (* PackWriter , error ) {
81
83
return newPackWrite (d .fs )
82
84
}
83
85
84
- // ObjectsPacks returns the list of availables packfiles
85
- func (d * DotGit ) ObjectsPacks () ([]fs. FileInfo , error ) {
86
+ // ObjectPacks returns the list of availables packfiles
87
+ func (d * DotGit ) ObjectPacks () ([]core. Hash , error ) {
86
88
packDir := d .fs .Join (objectsPath , packPath )
87
89
files , err := d .fs .ReadDir (packDir )
88
90
if err != nil {
@@ -93,43 +95,51 @@ func (d *DotGit) ObjectsPacks() ([]fs.FileInfo, error) {
93
95
return nil , err
94
96
}
95
97
96
- var packs []fs. FileInfo
98
+ var packs []core. Hash
97
99
for _ , f := range files {
98
- if strings .HasSuffix (f .Name (), packExt ) {
99
- packs = append ( packs , f )
100
+ if ! strings .HasSuffix (f .Name (), packExt ) {
101
+ continue
100
102
}
103
+
104
+ n := f .Name ()
105
+ h := core .NewHash (n [5 : len (n )- 5 ]) //pack-(hash).pack
106
+ packs = append (packs , h )
107
+
101
108
}
102
109
103
110
return packs , nil
104
111
}
105
112
106
- // ObjectPack returns the requested packfile and his idx
107
- func (d * DotGit ) ObjectPack (filename string ) (pack , idx fs.File , err error ) {
108
- if ! strings .HasSuffix (filename , packExt ) {
109
- return nil , nil , fmt .Errorf ("a .pack file should be provided" )
110
- }
113
+ // ObjectPack returns a fs.File of the given packfile
114
+ func (d * DotGit ) ObjectPack (hash core.Hash ) (fs.File , error ) {
115
+ file := d .fs .Join (objectsPath , packPath , fmt .Sprintf ("pack-%s.pack" , hash .String ()))
111
116
112
- pack , err = d .fs .Open (d . fs . Join ( objectsPath , packPath , filename ) )
117
+ pack , err : = d .fs .Open (file )
113
118
if err != nil {
114
119
if os .IsNotExist (err ) {
115
- return nil , nil , ErrPackfileNotFound
120
+ return nil , ErrPackfileNotFound
116
121
}
117
122
118
- return
123
+ return nil , err
119
124
}
120
125
121
- idxfile := filename [0 :len (filename )- len (packExt )] + idxExt
122
- idxpath := d .fs .Join (objectsPath , packPath , idxfile )
123
- idx , err = d .fs .Open (idxpath )
126
+ return pack , nil
127
+ }
128
+
129
+ // ObjectPackIdx returns a fs.File of the index file for a given packfile
130
+ func (d * DotGit ) ObjectPackIdx (hash core.Hash ) (fs.File , error ) {
131
+ file := d .fs .Join (objectsPath , packPath , fmt .Sprintf ("pack-%s.idx" , hash .String ()))
132
+
133
+ idx , err := d .fs .Open (file )
124
134
if err != nil {
125
135
if os .IsNotExist (err ) {
126
- return nil , nil , ErrIdxNotFound
136
+ return nil , ErrPackfileNotFound
127
137
}
128
138
129
- return
139
+ return nil , err
130
140
}
131
141
132
- return
142
+ return idx , nil
133
143
}
134
144
135
145
// Objects returns a slice with the hashes of objects found under the
@@ -194,14 +204,15 @@ func isHexAlpha(b byte) bool {
194
204
}
195
205
196
206
type PackWriter struct {
197
- fs fs.Filesystem
198
- sr io.ReadCloser
199
- sw io.WriteCloser
200
- fw fs.File
201
- mw io.Writer
202
- hash core.Hash
203
- index index.Index
204
- result chan error
207
+ fs fs.Filesystem
208
+ sr io.ReadCloser
209
+ sw io.WriteCloser
210
+ fw fs.File
211
+ mw io.Writer
212
+
213
+ checksum core.Hash
214
+ index idxfile.Idxfile
215
+ result chan error
205
216
}
206
217
207
218
func newPackWrite (fs fs.Filesystem ) (* PackWriter , error ) {
@@ -230,12 +241,23 @@ func newPackWrite(fs fs.Filesystem) (*PackWriter, error) {
230
241
231
242
func (w * PackWriter ) buildIndex () {
232
243
defer w .sr .Close ()
233
- index , hash , err := index .NewFromPackfile (w .sr )
244
+ o := memory .NewStorage ().ObjectStorage ()
245
+ s := packfile .NewScannerFromReader (w .sr )
246
+ d := packfile .NewDecoder (s , o )
247
+
248
+ checksum , err := d .Decode ()
249
+ if err != nil {
250
+ w .result <- err
251
+ return
252
+ }
234
253
235
- w .index = index
236
- w .hash = hash
254
+ w .checksum = checksum
255
+ w .index . PackfileChecksum = checksum
237
256
238
- fmt .Println (hash , w .index )
257
+ offsets := d .Offsets ()
258
+ for h , crc := range d .CRCs () {
259
+ w .index .Add (h , uint64 (offsets [h ]), crc )
260
+ }
239
261
240
262
w .result <- err
241
263
}
@@ -265,9 +287,26 @@ func (w *PackWriter) Close() error {
265
287
}
266
288
267
289
func (w * PackWriter ) save () error {
268
- base := w .fs .Join (objectsPath , packPath , fmt .Sprintf ("pack-%s" , w .hash ))
290
+ base := w .fs .Join (objectsPath , packPath , fmt .Sprintf ("pack-%s" , w .checksum ))
291
+
292
+ idx , err := w .fs .Create (fmt .Sprintf ("%s.idx" , base ))
293
+ if err != nil {
294
+ return err
295
+ }
269
296
270
- //idx, err := w.fs.Create(fmt.Sprintf("%s.idx", base))
297
+ if err := w .encodeIdx (idx ); err != nil {
298
+ return err
299
+ }
300
+
301
+ if err := idx .Close (); err != nil {
302
+ return err
303
+ }
271
304
272
305
return w .fs .Rename (w .fw .Filename (), fmt .Sprintf ("%s.pack" , base ))
273
306
}
307
+
308
+ func (w * PackWriter ) encodeIdx (writer io.Writer ) error {
309
+ e := idxfile .NewEncoder (writer )
310
+ _ , err := e .Encode (& w .index )
311
+ return err
312
+ }
0 commit comments