From d72a19796ef0f556db93b553547f2ac085b59a1a Mon Sep 17 00:00:00 2001 From: sona-tar Date: Wed, 31 Aug 2016 05:13:12 +0900 Subject: Support non packed objects (#68) * Support non packed git objects * Support non packed git objects for Iterator * Fix error handling from Writer() in FillObject() * Fix format in func (r *Reader) FillObject(obj core.Object) error * Fix to return d.addRefsFromPackedRefs() error And if packed-refs dosen't exist not to return error in d.addRefsFromPackedRefs * Remove debug code * Add GoDoc for func (d *DotGit) Objectfile(h core.Hash) (fs.FS, string, error) * Add GoDoc for func (r *Reader) FillObject(obj core.Object) error * Add GoDoc for func (d *DotGit) Objectfiles() (fs.FS, []core.Hash, error) * Fix format in func (d *DotGit) Objectfile(h core.Hash) (fs.FS, string, error) * Rename value dotGitobjcts -> objsDir * Change regexp.Compile -> regexp.MustCompile * Move regexp to variable initialization * Rename regexp value to be more coherent * Fix object directory name and object file name to correct character * Faster Objectfiles func * Add test for FillObject * Add GoDoc for func (s *ObjectStorage) Get(h core.Hash) (core.Object, error) * defer Close() * Return name values for defer function overwrite the error value. * Fix error handling in func (s *ObjectStorage) Get() Return error that gets error except for ErrObjfileNotFound from getFromUnpacked() * Rename getFromObject -> getFromUnpacked * Add test for func (d *DotGit) Objectfile(h core.Hash) (fs.FS, string, error) * Add test for func (d *DotGit) Objectfiles() (fs.FS, []core.Hash, error) * Faster check git object name * Faster dotgit_test.go * Fix Godoc for Objectfiles func * Refactor variable name in Objectfiles func * Fix GoDoc for objectfile func * Fix TestObjectfile func and TestObjectfiles func * Rename fixobj -> fixObj in Test Objectfile func * Fix test compare method * Refactor Get func in object.go * Refactor getFromUnpacked func in object.go * Fix GoDoc for ErrObjfileNotFound * Fix TestObjectfiles for not guarantee the slice order * Change error no such file or directory to target file not found * Change spec func (s *ObjectStorage) Get(h core.Hash) (core.Object, error) return core.ErrObjectNotFound, if index pointer is nil. * Add space * storage: Add object type hint parameter to ObjectStorage.getFromUnpacked --- storage/filesystem/internal/dotgit/dotgit.go | 82 ++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) (limited to 'storage/filesystem/internal/dotgit/dotgit.go') diff --git a/storage/filesystem/internal/dotgit/dotgit.go b/storage/filesystem/internal/dotgit/dotgit.go index 448f6a2..75c98ff 100644 --- a/storage/filesystem/internal/dotgit/dotgit.go +++ b/storage/filesystem/internal/dotgit/dotgit.go @@ -22,6 +22,8 @@ var ( ErrIdxNotFound = errors.New("idx file not found") // ErrPackfileNotFound is returned by Packfile when the packfile is not found ErrPackfileNotFound = errors.New("packfile not found") + // ErrObjfileNotFound is returned by Objectfile when the objectffile is not found + ErrObjfileNotFound = errors.New("object file not found") // ErrConfigNotFound is returned by Config when the config is not found ErrConfigNotFound = errors.New("config file not found") ) @@ -74,6 +76,10 @@ func (d *DotGit) Packfile() (fs.FS, string, error) { packDir := d.fs.Join(d.path, "objects", "pack") files, err := d.fs.ReadDir(packDir) if err != nil { + if os.IsNotExist(err) { + return nil, "", ErrPackfileNotFound + } + return nil, "", err } @@ -93,6 +99,10 @@ func (d *DotGit) Idxfile() (fs.FS, string, error) { packDir := d.fs.Join(d.path, "objects", "pack") files, err := d.fs.ReadDir(packDir) if err != nil { + if os.IsNotExist(err) { + return nil, "", ErrIdxNotFound + } + return nil, "", err } @@ -118,3 +128,75 @@ func (d *DotGit) Config() (fs.FS, string, error) { return d.fs, configFile, nil } + +// Objectfiles returns a slice with the hashes of objects found under the +// .git/objects/ directory. +func (dg *DotGit) Objectfiles() (fs.FS, []core.Hash, error) { + objsDir := dg.fs.Join(dg.path, "objects") + + files, err := dg.fs.ReadDir(objsDir) + if err != nil { + if os.IsNotExist(err) { + return nil, nil, ErrObjfileNotFound + } + + return nil, nil, err + } + + var objects []core.Hash + for _, f := range files { + if f.IsDir() && len(f.Name()) == 2 && isHex(f.Name()) { + objDir := f.Name() + d, err := dg.fs.ReadDir(dg.fs.Join(objsDir, objDir)) + if err != nil { + return nil, nil, err + } + + for _, o := range d { + objects = append(objects, core.NewHash(objDir+o.Name())) + } + } + } + + return dg.fs, objects, nil +} + +func isHex(s string) bool { + for _, b := range []byte(s) { + if isNum(b) { + continue + } + if isHexAlpha(b) { + continue + } + + return false + } + + return true +} + +func isNum(b byte) bool { + return b >= '0' && b <= '9' +} + +func isHexAlpha(b byte) bool { + return b >= 'a' && b <= 'f' || b >= 'A' && b <= 'F' +} + +// Objectfile returns the path of the object file for a given hash +// *if the file exists*, otherwise returns an ErrObjfileNotFound error. +func (d *DotGit) Objectfile(h core.Hash) (fs.FS, string, error) { + hash := h.String() + objFile := d.fs.Join(d.path, "objects", hash[0:2], hash[2:40]) + + if _, err := d.fs.Stat(objFile); err != nil { + if os.IsNotExist(err) { + return nil, "", ErrObjfileNotFound + } + + return nil, "", err + } + + return d.fs, objFile, nil +} -- cgit