diff options
author | sona-tar <sona.zip@gmail.com> | 2016-08-31 05:13:12 +0900 |
---|---|---|
committer | Máximo Cuadros <mcuadros@gmail.com> | 2016-08-30 22:13:12 +0200 |
commit | d72a19796ef0f556db93b553547f2ac085b59a1a (patch) | |
tree | 0c5cc404852b0fd46d35f50cbb3afddff49f7018 /storage/filesystem/internal/dotgit/dotgit.go | |
parent | 6d846503013f5a9cde0597698c998b6c0e0dc055 (diff) | |
download | go-git-d72a19796ef0f556db93b553547f2ac085b59a1a.tar.gz |
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
Diffstat (limited to 'storage/filesystem/internal/dotgit/dotgit.go')
-rw-r--r-- | storage/filesystem/internal/dotgit/dotgit.go | 82 |
1 files changed, 82 insertions, 0 deletions
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 +} |